Nginx HTTP(S) Reverse Proxy/Web Server

This post with list of typical nginx configurations and example configuration snippets for these configurations.

All sample configuration files are tested on up to date version of nginx, which has been compiled and installed with following commands:

# ./configure –prefix=/usr/local/nginx –with-http_ssl_module
# make
# make install

Using nginx as web server with PHP support

If you need to use nginx with PHP, you can setup PHP as FastCGI and let nginx to forward all PHP queries to some FastCGI port (tcp/socket). To use this configuration you need to start PHP as FastCGI using some third party software like spawn-fcgi from lighttpd

To enable PHP support, you need to add special location section to your config file:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass   127.0.0.1:12345;
fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
fastcgi_param  QUERY_STRING     $query_string;
fastcgi_param  REQUEST_METHOD   $request_method;
fastcgi_param  CONTENT_TYPE     $content_type;
fastcgi_param  CONTENT_LENGTH   $content_length;
}

——————-
Using nginx as web server with SSI support

Server-Side Includes (aka SSI) is another interesting feature of nginx. As for now, following ssi instructions are supported: config, echo, if, include, set.

SSI support can be anabled by single line configuration command in your config file:

location / {
ssi  on;

}
Using nginx as https-enabled web server

You need https-access to your Nginx-powered site? No problems! Nginx supports https and can be used to implement secured web-server with SSLv2, SSLv3 or TLSv1.

To enable https-support you should have certificate and key files. How to obtain them, you can read in SSL FAQ. When you will obtain them, you can enable ssl-module:

server {
listen               443;
ssl                  on;
ssl_certificate      /usr/local/nginx/conf/cert.pem;
ssl_certificate_key  /usr/local/nginx/conf/cert.key;
keepalive_timeout    70;

}
Using nginx as reverse-proxy server before some another web-server

If you have some large web-site and you have noticed, that your Apache can not handle more load, you can put nginx before your primary web-server to use it as light reverse-proxy and as web-server to handle requests to static files.

Thanks to nginx flexibility, you can pass any types of requests to backend server by using location sections (all files, only dynamic content requests or some specific locations in your web-server tree):

location / {
proxy_pass        http://localhost:8000/;
proxy_set_header  X-Real-IP  $remote_addr;
}
Using nginx for virtual hosting platforms

One of the interesting use cases for Nginx is virtual hosting platform because it meets all requirements for good hosting server: it is efficient, it supports all popular virtual hosting methods and it has very good internal structure, so it can be easily extended in for any specific areas.

As for now, it is being used by many hosting companies as reverse proxy and I am using it on my free hosting service with millions unique visitors per day.

If you vant to try virtual hosting feature, you can create additional server sections in your config file (first section will be default):

http {
server {
listen  192.168.10.1;
listen  192.168.10.1:8000;

server_name   one.example.com  http://www.one.example.com;

}

server {
listen  192.168.10.1;
listen  192.168.10.2:8000;
listen  9000;

server_name   two.example.com  http://www.two.example.com
three.example.com  http://www.three.example.com;

}

server {
listen  9000;

server_name   four.example.com  http://www.four.example.com;

}
}

 Quoted from
This post with list of typical nginx configurations and example configuration snippets for these configurations.

All sample configuration files are tested on up to date version of nginx, which has been compiled and installed with following commands:

# ./configure –prefix=/usr/local/nginx –with-http_ssl_module
# make
# make install

Using nginx as web server with PHP support

If you need to use nginx with PHP, you can setup PHP as FastCGI and let nginx to forward all PHP queries to some FastCGI port (tcp/socket). To use this configuration you need to start PHP as FastCGI using some third party software like spawn-fcgi from lighttpd

To enable PHP support, you need to add special location section to your config file:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass   127.0.0.1:12345;
fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
fastcgi_param  QUERY_STRING     $query_string;
fastcgi_param  REQUEST_METHOD   $request_method;
fastcgi_param  CONTENT_TYPE     $content_type;
fastcgi_param  CONTENT_LENGTH   $content_length;
}

——————-
Using nginx as web server with SSI support

Server-Side Includes (aka SSI) is another interesting feature of nginx. As for now, following ssi instructions are supported: config, echo, if, include, set.

SSI support can be anabled by single line configuration command in your config file:

location / {
ssi  on;

}
Using nginx as https-enabled web server

You need https-access to your Nginx-powered site? No problems! Nginx supports https and can be used to implement secured web-server with SSLv2, SSLv3 or TLSv1.

To enable https-support you should have certificate and key files. How to obtain them, you can read in SSL FAQ. When you will obtain them, you can enable ssl-module:

server {
listen               443;
ssl                  on;
ssl_certificate      /usr/local/nginx/conf/cert.pem;
ssl_certificate_key  /usr/local/nginx/conf/cert.key;
keepalive_timeout    70;

}
Using nginx as reverse-proxy server before some another web-server

If you have some large web-site and you have noticed, that your Apache can not handle more load, you can put nginx before your primary web-server to use it as light reverse-proxy and as web-server to handle requests to static files.

Thanks to nginx flexibility, you can pass any types of requests to backend server by using location sections (all files, only dynamic content requests or some specific locations in your web-server tree):

location / {
proxy_pass        http://localhost:8000/;
proxy_set_header  X-Real-IP  $remote_addr;
}
Using nginx for virtual hosting platforms

One of the interesting use cases for Nginx is virtual hosting platform because it meets all requirements for good hosting server: it is efficient, it supports all popular virtual hosting methods and it has very good internal structure, so it can be easily extended in for any specific areas.

As for now, it is being used by many hosting companies as reverse proxy and I am using it on my free hosting service with millions unique visitors per day.

If you vant to try virtual hosting feature, you can create additional server sections in your config file (first section will be default):

http {
server {
listen  192.168.10.1;
listen  192.168.10.1:8000;

server_name   one.example.com  http://www.one.example.com;

}

server {
listen  192.168.10.1;
listen  192.168.10.2:8000;
listen  9000;

server_name   two.example.com  http://www.two.example.com
three.example.com  http://www.three.example.com;

}

server {
listen  9000;

server_name   four.example.com  http://www.four.example.com;

}

)
 Quoted from blog.kovyrin.net

Leave a comment