Saturday, November 30, 2013

Proxy Pass Gitblit and Nexus in Nginx

I have done this set-up several weeks ago. I just want to share this to anyone who's looking for some steps to set-up the same pool of technologies in his machine.

I have been using Apache 2 as my web server for more than a year now, but I still don't understand how to set it up. I'm not sure if I'm just too slow for Apache 2 or its documentation just lack the details. When I started using Nginx, I thought I never come back to Apache 2 anymore. And, it's really true. Nginx is a lot better to understand and it just took me some time to set-up and configure my server again.

What I will show is how I pass proxy my Gitblit and Nexus applications.

Ngix Location Patterns

Before I show you how I pass proxy my applications, please read the following excerpts from Nginx documentation site.

location  = / {
  # matches the query / only.
  [ configuration A ] 
}
location  / {
  # matches any query, since all queries begin with /, but regular
  # expressions and any longer conventional blocks will be
  # matched first.
  [ configuration B ] 
}
location /documents/ {
  # matches any query beginning with /documents/ and continues searching,
  # so regular expressions will be checked. This will be matched only if
  # regular expressions don't find a match.
  [ configuration C ] 
}
location ^~ /images/ {
  # matches any query beginning with /images/ and halts searching,
  # so regular expressions will not be checked.
  [ configuration D ] 
}
location ~* \.(gif|jpg|jpeg)$ {
  # matches any request ending in gif, jpg, or jpeg. However, all
  # requests to the /images/ directory will be handled by
  # Configuration D.   
  [ configuration E ] 
}

You may read this part at http://wiki.nginx.org/HttpCoreModule.

Nginx Configuration Folders

I'm using Linux so my installation of Nginx might be a little different with that of Windows. But anyways, the way to configure Nginx must still be the same. The following is the content of the Nginx configuration folder (/etc/nginx).

total 80
drwxr-xr-x   5 root root  4096 Nov 12 06:24 ./
drwxr-xr-x 131 root root 12288 Nov 24 00:15 ../
drwxr-xr-x   2 root root  4096 May 10  2013 conf.d/
-rw-r--r--   1 root root   898 Apr 29  2013 fastcgi_params
-rw-r--r--   1 root root  2258 Apr 29  2013 koi-utf
-rw-r--r--   1 root root  1805 Apr 29  2013 koi-win
-rw-r--r--   1 root root  2085 Apr 29  2013 mime.types
-rw-r--r--   1 root root  5287 Apr 29  2013 naxsi_core.rules
-rw-r--r--   1 root root   287 Apr 29  2013 naxsi.rules
-rw-r--r--   1 root root   222 Apr 29  2013 naxsi-ui.conf
-rw-r--r--   1 root root  1644 Nov 27 08:03 nginx.conf
-rw-r--r--   1 root root   131 Apr 29  2013 proxy_params
-rw-r--r--   1 root root   465 Apr 29  2013 scgi_params
drwxr-xr-x   2 root root  4096 Nov 26 23:48 sites-available/
drwxr-xr-x   2 root root  4096 Nov 24 00:27 sites-enabled/
-rw-r--r--   1 root root   532 Apr 29  2013 uwsgi_params
-rw-r--r--   1 root root  3071 Apr 29  2013 win-utf

The main configuration file here is nginx.conf. If you open this file, you will find a few configuration blocks, but will focus on the HTTP configuration since we are talking about web application here. Note that Nginx can also pass proxy other stuff like SMTP.

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-      
        # javascript text/xml ap$

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

Notice the last line with the keyword include. This statement says that it will include some configuration from configuration files under the folder /etc/nginx/sites-enabled.

Initially after installing Nginx, you will find a file named default under /etc/nginx/sites-enabled. This file contains server block configuration like the following:

server {
     listen 80;
     server_name my.server.com
     
     location / {
               root 8/usr/share/nginx/html;
        index index.html index.htm;          
     }
}

By calling include in nginx.conf, the effective configuration will be something like the following:

http {
     ...
     ...
     server {
          listen 80;
          server_name my.server.com
     
          location / {
                         root 8/usr/share/nginx/html;
            index index.html index.htm;          
          }
     }
}

For my purpose, I have removed the default file and created my own configuration file. I created myserver configuration file at /etc/nginx/sites-enabled and here are the contents:

server {
        listen          80;
        server_name     my.server.com;

        location / {
                proxy_pass           http://myserver.azurewebsites.net;
        }

        location ^~ /gitblit/ {
                proxy_pass           http://localhost:18080;
                proxy_set_header     X-Real-IP $remote_addr;
                proxy_set_header     X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header     Host $http_host;
        }

        location ^~ /nexus/ {
                proxy_pass           http://localhost:18090;
                proxy_set_header     X-Real-IP $remote_addr;
                proxy_set_header     X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header     Host $http_host;
        }

        location ^~ /application3/ {
                proxy_pass           http://localhost:18600;
                proxy_set_header     X-Real-IP $remote_addr;
                proxy_set_header     X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header     Host $http_host;
        }

        location ^~ /application4/ {
                proxy_pass           http://localhost:18700;
                proxy_set_header     X-Real-IP $remote_addr;
                proxy_set_header     X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header     Host $http_host;
        }
}

I have about six location blocks. All of these points to the respective Jetty server instances that I have created some weeks ago, except for one. The root / points to the Windows Azure website that I have recently created. I separate this application for some purpose that I will not mention here. All will be accessed via Nginx or via port 80.

That's all. I hope you get something from here.
Thanks for reading.

No comments:

Post a Comment