Mischiefblog
I make apps for other people

HOWTO proxy with nginx

Posted by Chris Jones
On July 25th, 2012 at 13:51

Permalink | Trackback | Links In |

Comments Off on HOWTO proxy with nginx
Posted in General

These instructions assume you’re using a Debian/Ubuntu-based system.

  1. Create a web service/app (i.e., a Maven CXF JAX-RS archetype, or Flask/Bottle/Django, etc.)
  2. Install curl
    sudo apt-get install curl
  3. Install nginx
    sudo apt-get install nginx-full
    which should include nginx-common as a dependency.
  4. Do not start nginx yet.
  5. Add the nginx load balancer configuration and mime.types to your web service project (called MyProject in the following examples)
    1. Copy the mime.types definition file (as an alternative to copying the nginx configuration to /etc/nginx)
      cp /etc/nginx/mime.type ~/workspace/MyProject/web/conf
    2. Copy and modify the following load balancer/proxy configuration into MyProject/web/conf/balancer.nginx

      worker_processes 1;
          
      events {
          worker_connections 64;
          # multi_accept on;
      }
          
      http {
          upstream service-backend {
              server 127.0.0.1:9260;
              # server 127.0.0.1:8102;
              # server 127.0.0.1:8103;
              # server 127.0.0.1:8104;
          }
          
          keepalive_timeout 300 300;
          charset utf-8;
          default_type application/octet-stream;
          ignore_invalid_headers on;
          include mime.types;
          keepalive_requests 20;
          recursive_error_pages on;
          sendfile on;
          server_tokens off;
          source_charset utf-8;
          gzip on;
          gzip_static on;
          
          log_format main '$remote_addr $host $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $ssl_cipher $request_time';
          
          server {
              listen 127.0.0.1:8100;
              server_name mydesktop.domain.com mydesktop;
              add_header Cache-Control public;
              access_log /var/log/nginx/access.log main buffer=32k;
              error_log /var/log/nginx/error.log error buffer=8k;
              expires max;
              root /opt/MyProject/web/html;
          
              location /svc/ {
                  proxy_pass http://127.0.0.1:9260;
              }
          
              location / {
              
              }
          
              location /favicon.ico {
                  return 204;
              }
              }
      }
  6. Create a directory to hold your static web content (HTML pages, images, JavaScript)
    mkdir MyProject/web/html
  7. Create a placeholder HTML file in the static web content/html directory
  8. Set your webservice to run on the /svc URI endpoint (to match the nginx configuration)
    1. Update the MyProject integration tests

      <parameter name="com.domain.webservices.AppRest.url"
      type="text" desc="Example service URL">
      %<value>http://localhost:9260/MyProject/svc</value>
      </parameter>
    2. Update the MyProject/web/conf/web.xml to map the Jersey web application to “/svc/*” (or whatever is appropriate for your app)
      <servlet-mapping>
      <servlet-name>Jersey Web Application/svc/*
  9. Build your project
    cd ~/workspace/MyProject
    mvn clean install
  10. Install and start your app (under /opt, for instance)
  11. Start nginx as root
    nginx -c /opt/Myproject/web/conf/balancer.nginx
  12. Verify your deployment
    1. Check your placeholder HTML page
      curl http://localhost:8100/index.html
    2. Check your service health
      curl http://localhost:8100/svc/health

Comments are closed.