Running Nginx as Reverse Proxy server October 30 2011 This is a tutorial on running Nginx as a reverse proxy server. It covers all basics of using Nginx as a reverse proxy server. By Ayodhyanath Guru www.jafaloo.com Copyright 2011-12 @ www.jafaloo.com Page 0
Running Nginx as a Reverse Proxy Server Index 1. Introduction 2. Requirements 3. Download Nginx 4. Compile Nginx for reverse proxy 5. Configure Nginx for reverse proxy 6. Conclusion Copyright 2011-12 @ www.jafaloo.com Page 1
Introduction This is a PDF version of the tutorial how to run Nginx as a reverse proxy server. This tutorial covers all basics of running Nginx as reverse proxy server. Here I assume that you are already familiar with Linux, Nginx and shell scripting. Nginx is one of the most popular and fast web servers. Nginx can be used as a load balancer, a proxy server, a reverse proxy server, an http server, an IMAP and POP3 proxy server. Requirements Below is a list of the requirements that needs to be fulfilled before installing Nginx. You can install Nginx on Windows system also but this article is only intended for using Nginx on Linux system. 1. A Linux system 2. PCRE Library installed 3. Nginx source code 4. System should be installed with Open SSL (For SSL support) (Optional) 5. Root access to the Linux system is required. Download Nginx First of all you need to download the source code of latest stable Nginx from http://nginx.org/. The latest stable version of Nginx was 1.0.8 when this article was written. So go ahead and download the latest stable source code whichever is available. Compile Nginx for Reverse Proxy First of all you need to login to the system with root user. If PCRE library is not yet installed then install it first. After installing PCRE library, follow the below steps to compile and install Nginx as a proxy server. STEP 1: Unzip the Nginx source code that you have downloaded to the directory /usr/local/src/ STEP 2: Change the directory to Nginx home folder. Like cd nginx-1.0.8 Copyright 2011-12 @ www.jafaloo.com Page 2
STEP 3: By default Nginx will be installed in /usr/local/nginx directory. You can also change the default installation location while configuring the installation. Now from the source directory execute the below command, remember this is a single line:./configure --pid-path=/var/run/nginx.pid \ --with-http_ssl_module \ --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log Note: The line marked with red color is optional and if you do not want SSL support then you can omit that line. STEP 4: From the same source directory execute the command as shown below: make && make install This will install Nginx in the /usr/local/nginx directory. You can find the stand alone Nginx executable in the directory /us/local/nginx/sbin/. The configuration file is located at the location: /usr/local/conf/nginx.conf. Configure Nginx for Reverse Proxy By default Nginx will create a configuration file after installation. This configuration file can be used as a base file for further configuration. The below nginx.conf file is a sample one for you so that you can modify it according to your use. This sample configuration has been taken from Nginx wiki site. user www www; worker_processes 5; error_log logs/error.log; pid logs/nginx.pid; worker_rlimit_nofile 8192; events { worker_connections 4096; http { include conf/mime.types; include /etc/nginx/proxy.conf; include /etc/nginx/fastcgi.conf; index index.html index.htm index.php; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' Copyright 2011-12 @ www.jafaloo.com Page 3
'"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; tcp_nopush on; server_names_hash_bucket_size 128; # this seems to be required for some vhosts server { # php/fastcgi listen 80; server_name domain1.com www.domain1.com; access_log logs/domain1.access.log main; root html; location ~ \.php$ { fastcgi_pass 127.0.0.1:1025; server { # simple reverse-proxy listen 80; server_name domain2.com www.domain2.com; access_log logs/domain2.access.log main; # serve static files location ~ ^/(images javascript js css flash media static)/ { root /var/www/virtual/big.server.com/htdocs; expires 30d; # pass requests for dynamic content to rails/turbogears/zope, et al location / { proxy_pass http://127.0.0.1:8080; upstream big_server_com { server 127.0.0.3:8000 weight=5; server 127.0.0.3:8001 weight=5; server 192.168.0.1:8000; server 192.168.0.1:8001; server { # simple load balancing listen 80; server_name big.server.com; access_log logs/big.server.access.log main; Copyright 2011-12 @ www.jafaloo.com Page 4
location / { proxy_pass http://big_server_com; Now let s see the configuration for reverse proxy with cache Below http module is an example of how to configure Nginx as reverse proxy with caching: http { proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=static:10m inactive=24h max_size=1g; server { location / { proxy_pass http://1.2.3.4; proxy_set_header Host $host; proxy_cache STATIC; proxy_cache_valid 200 1d; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; In the above configuration we are defining the cache path with the parameter proxy_cache_path and inside the server module we are offloading the request to an http backend server with IP 1.2.3.4 (Parameter proxy_pass). With the proxy_cache parameter we are defining that Nginx should cache only static file (STATIC). With the above configuration Nginx will work as a reverse proxy server with caching enabled. For more information on different configuration options you can visit http://wiki.nginx.org. Conclusion Finally if you have any problems in installing or compiling or running Nginx as proxy server you can contact me at: jafaloo@jafaloo.com with your queries, I will try to solve them. If you need script to start, stop and restart Nginx please visit the download page @ http://www.jafaloo.com/2011/07/15/start-stop-script-for-nginx/. Note: Both the configuration examples have been taken from Nginx Wiki site. If you want to know more configuration options please visit: http://wiki.nginx.org Copyright 2011-12 @ www.jafaloo.com Page 5
Copyright 2011-12 @ www.jafaloo.com Page 6