{"id":2781,"date":"2014-04-09T07:14:27","date_gmt":"2014-04-09T07:14:27","guid":{"rendered":"http:\/\/blog.shineservers.com\/?p=2781"},"modified":"2014-04-09T07:14:27","modified_gmt":"2014-04-09T07:14:27","slug":"use-nginx-reverse-proxy-server","status":"publish","type":"post","link":"https:\/\/www.shineservers.com\/2014\/04\/09\/use-nginx-reverse-proxy-server\/","title":{"rendered":"How To Use Nginx As Reverse Proxy Server"},"content":{"rendered":"<p>Nginx is an open source Web server and a reverse proxy server. You can use nginx for a load balancing and\/or as a proxy solution to run services from inside those machines through your host&#8217;s single public IP address such as 202.54.1.1. In this post, I will explain how to install nginx as reverse proxy server for Apache+php5 domain called www.example.com and Lighttpd static asset domain called static.example.com. You need to type the following commands on\u00a0<strong>vm00<\/strong>having an IP address\u00a0<strong>192.168.1.1<\/strong>\u00a0only.<\/p>\n<h2>DNS Setup<\/h2>\n<p>Make sure both www.example.com and static.example.com point to public IP address 202.54.1.1.<\/p>\n<h2>Install nginx server<\/h2>\n<p>Type the following command to install nginx web server:<br \/>\n<code>$ cd \/tmp<br \/>\n$ wget http:\/\/nginx.org\/packages\/rhel\/6\/noarch\/RPMS\/nginx-release-rhel-6-0.el6.ngx.noarch.rpm<br \/>\n# rpm -iv nginx-release-rhel-6-0.el6.ngx.noarch.rpm<br \/>\n# yum install nginx<\/code><br \/>\nSample outputs:<\/p>\n<pre>Loaded plugins: rhnplugin\nSetting up Install Process\nResolving Dependencies\n--&gt; Running transaction check\n---&gt; Package nginx.x86_64 0:1.2.1-1.el6.ngx will be installed\n--&gt; Finished Dependency Resolution\nDependencies Resolved\n=========================================================================\n Package      Arch          Version                   Repository    Size\n=========================================================================\nInstalling:\n nginx        x86_64        1.2.1-1.el6.ngx           nginx        331 k\nTransaction Summary\n=========================================================================\nInstall       1 Package(s)\nTotal download size: 331 k\nInstalled size: 730 k\nIs this ok [y\/N]: y\nDownloading Packages:\nnginx-1.2.1-1.el6.ngx.x86_64.rpm                  | 331 kB     00:00\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\nWarning: RPMDB altered outside of yum.\n  Installing : nginx-1.2.1-1.el6.ngx.x86_64                          1\/1\n----------------------------------------------------------------------\nThanks for using NGINX!\nCheck out our community web site:\n* http:\/\/nginx.org\/en\/support.html\nIf you have questions about commercial support for NGINX please visit:\n* http:\/\/www.nginx.com\/support.html\n----------------------------------------------------------------------\n  Verifying  : nginx-1.2.1-1.el6.ngx.x86_64                          1\/1\nInstalled:\n  nginx.x86_64 0:1.2.1-1.el6.ngx\nComplete!<\/pre>\n<h2>Configure the nginx web server as reverse proxy<\/h2>\n<p>Edit \/etc\/nginx\/conf.d\/default.conf, enter:<br \/>\n<code># vi \/etc\/nginx\/conf.d\/default.conf<\/code><br \/>\nAdd\/correct as follows:<\/p>\n<pre>\u00a0\n## Basic reverse proxy server ##\n## Apache (vm02) backend for www.example.com ##\nupstream apachephp  {\n      server 192.168.1.11:80; #Apache1\n}\n\n## Lighttpd (vm01) backend for static.example.com ##\nupstream lighttpd  {\n      server 192.168.1.10:80; #Lighttpd1\n}\n\n## Start www.example.com ##\nserver {\n    listen       202.54.1.1:80;\n    server_name  www.example.com;\n\n    access_log  \/var\/log\/nginx\/log\/www.example.access.log  main;\n    error_log  \/var\/log\/nginx\/log\/www.example.error.log;\n    root   \/usr\/share\/nginx\/html;\n    index  index.html index.htm;\n\n    ## send request back to apache1 ##\n    location \/ {\n     proxy_pass  http:\/\/apachephp;\n     proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;\n     proxy_redirect off;\n     proxy_buffering off;\n     proxy_set_header        Host            $host;\n     proxy_set_header        X-Real-IP       $remote_addr;\n     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;\n   }\n}\n## End www.example.com ##\n\n## START static.example.com ##\nserver {\n   listen      202.54.1.1:80;\n   server_name static.example.com;\n   access_log  \/var\/log\/nginx\/log\/static.example.com.access.log  main;\n   error_log   \/var\/log\/nginx\/log\/static.example.com.error.log;\n   root        \/usr\/local\/nginx\/html;\n   index       index.html;\n\n   location \/ {\n        proxy_pass  http:\/\/lighttpd;\n        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;\n        proxy_redirect off;\n        proxy_buffering off;\n        proxy_set_header        Host            static.example.com;\n        proxy_set_header        X-Real-IP       $remote_addr;\n        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;\n    }\n}\n## END static.example.com  ##<\/pre>\n<h3>Turn on Nginx<\/h3>\n<p>Type the following commands:<br \/>\n<code># chkconfig nginx on<br \/>\n# service nginx start<\/code><\/p>\n<h3>Configure firewall<\/h3>\n<p>Set firewall as follows:<\/p>\n<ul>\n<li>Drop all INPUT\/OUTPUT chain traffic by default.<\/li>\n<li>Only open tcp port 202.54.1.1:80 and\/or 443 on eth0 only.<\/li>\n<li>Set eth1 as trusted device so that communication take place between nginx reverse proxy and Apache\/Lighttpd backend servers.<\/li>\n<\/ul>\n<p>Run the following command to set and customize firewall as described above:<br \/>\n<code># system-config-firewall-tui<\/code><br \/>\nYou can edit \/etc\/sysconfig\/iptables manually and set the firewall too.<\/p>\n<h3>\/etc\/sysctl.conf<\/h3>\n<p>Edit \/etc\/sysctl.conf as follows:<\/p>\n<pre>\u00a0\n# Execshild\nkernel.exec-shield = 1\nkernel.randomize_va_space = 1\n\n# IPv4 settings\nnet.ipv4.ip_forward = 0\nnet.ipv4.conf.all.send_redirects = 0\nnet.ipv4.conf.default.send_redirects = 0\nnet.ipv4.conf.all.accept_source_route = 0\nnet.ipv4.conf.all.accept_redirects = 0\nnet.ipv4.conf.all.secure_redirects = 0\nnet.ipv4.conf.all.log_martians = 1\nnet.ipv4.conf.default.accept_source_route = 0\nnet.ipv4.conf.default.accept_redirects = 0\nnet.ipv4.conf.default.secure_redirects = 0\nnet.ipv4.icmp_echo_ignore_broadcasts = 1\nnet.ipv4.tcp_syncookies = 1\nnet.ipv4.conf.all.rp_filter = 1\nnet.ipv4.conf.default.rp_filter = 1\n\n# Increase system file descriptor limit to\nfs.file-max = 50000\n\n# Increase system IP port limits\nnet.ipv4.ip_local_port_range = 2000 65000\n\n# Ipv6\nnet.ipv6.conf.default.router_solicitations = 0\nnet.ipv6.conf.default.accept_ra_rtr_pref = 0\nnet.ipv6.conf.default.accept_ra_pinfo = 0\nnet.ipv6.conf.default.accept_ra_defrtr = 0\nnet.ipv6.conf.default.autoconf = 0\nnet.ipv6.conf.default.dad_transmits = 0\nnet.ipv6.conf.default.max_addresses = 1<\/pre>\n<p>Load new Linux kernel settings, run:<br \/>\n<code># sysctl -p<\/code><\/p>\n<h3 class=\"zemanta-related-title\" style=\"margin: 0 0 10px 0; padding: 0; clear: both;\">Related articles across the web<\/h3>\n<ul class=\"zemanta-article-ul zemanta-article-ul-image\" style=\"margin: 0; padding: 0; overflow: hidden;\">\n<li class=\"zemanta-article-ul-li-image zemanta-article-ul-li\" style=\"padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 104px; font-size: 12px; margin: 0 5px 10px 0;\"><a style=\"padding: 2px; display: block; text-decoration: none;\" href=\"http:\/\/blog.shineservers.com\/install-squid-proxy-centos-6\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" style=\"border-radius: 3px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); padding: 0; margin: 0; border: 0; display: block; width: 100px; max-width: 100%;\" alt=\"\" src=\"http:\/\/i.zemanta.com\/232740940_150_150.jpg\" \/><\/a><a style=\"display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;\" href=\"http:\/\/blog.shineservers.com\/install-squid-proxy-centos-6\/\" target=\"_blank\" rel=\"noopener noreferrer\">How To Install Squid Proxy on CentOS 6<\/a><\/li>\n<li class=\"zemanta-article-ul-li-image zemanta-article-ul-li\" style=\"padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 104px; font-size: 12px; margin: 0 5px 10px 0;\"><a style=\"padding: 2px; display: block; text-decoration: none;\" href=\"http:\/\/blog.shineservers.com\/using-two-ips-bind-lighttpd-virtual-hosting\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" style=\"border-radius: 3px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); padding: 0; margin: 0; border: 0; display: block; width: 100px; max-width: 100%;\" alt=\"\" src=\"http:\/\/i.zemanta.com\/232740945_150_150.jpg\" \/><\/a><a style=\"display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;\" href=\"http:\/\/blog.shineservers.com\/using-two-ips-bind-lighttpd-virtual-hosting\/\" target=\"_blank\" rel=\"noopener noreferrer\">Using More Than Two IPs (bind) In Lighttpd For Virtual Hosting<\/a><\/li>\n<li class=\"zemanta-article-ul-li-image zemanta-article-ul-li\" style=\"padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 104px; font-size: 12px; margin: 0 5px 10px 0;\"><a style=\"padding: 2px; display: block; text-decoration: none;\" href=\"http:\/\/blog.shineservers.com\/update-install-packages-redhat-enterprise-centos-linux-version-6-x\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" style=\"border-radius: 3px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); padding: 0; margin: 0; border: 0; display: block; width: 100px; max-width: 100%;\" alt=\"\" src=\"http:\/\/i.zemanta.com\/261534561_150_150.jpg\" \/><\/a><a style=\"display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;\" href=\"http:\/\/blog.shineservers.com\/update-install-packages-redhat-enterprise-centos-linux-version-6-x\/\" target=\"_blank\" rel=\"noopener noreferrer\">Update \/ Install Packages Under Redhat Enterprise \/ CentOS Linux Version 6.x<\/a><\/li>\n<li class=\"zemanta-article-ul-li-image zemanta-article-ul-li\" style=\"padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 104px; font-size: 12px; margin: 0 5px 10px 0;\"><a style=\"padding: 2px; display: block; text-decoration: none;\" href=\"http:\/\/blog.shineservers.com\/optimise-mysql-apache-cpanelwhm\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" style=\"border-radius: 3px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); padding: 0; margin: 0; border: 0; display: block; width: 100px; max-width: 100%;\" alt=\"\" src=\"http:\/\/i.zemanta.com\/261167979_150_150.jpg\" \/><\/a><a style=\"display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;\" href=\"http:\/\/blog.shineservers.com\/optimise-mysql-apache-cpanelwhm\/\" target=\"_blank\" rel=\"noopener noreferrer\">How To Optimise MySQL &amp; Apache On cPanel\/WHM<\/a><\/li>\n<li class=\"zemanta-article-ul-li-image zemanta-article-ul-li\" style=\"padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 104px; font-size: 12px; margin: 0 5px 10px 0;\"><a style=\"padding: 2px; display: block; text-decoration: none;\" href=\"http:\/\/blog.shineservers.com\/install-geoip-module-countrycity-level-geo-targeting-nginx\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" style=\"border-radius: 3px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); padding: 0; margin: 0; border: 0; display: block; width: 100px; max-width: 100%;\" alt=\"\" src=\"http:\/\/i.zemanta.com\/232740952_150_150.jpg\" \/><\/a><a style=\"display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;\" href=\"http:\/\/blog.shineservers.com\/install-geoip-module-countrycity-level-geo-targeting-nginx\/\" target=\"_blank\" rel=\"noopener noreferrer\">Install GeoIP Module For Country\/City Level Geo Targeting (NGINX)<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Nginx is an open source Web server and a reverse proxy server. You can use nginx for a load balancing and\/or as a proxy solution to run services from inside those machines through your host&#8217;s single public IP address such as 202.54.1.1. In this post, I will explain how to install nginx as reverse proxy [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[53,60],"tags":[198],"class_list":["post-2781","post","type-post","status-publish","format-standard","hentry","category-general","category-linux","tag-how-to-use-nginx-as-reverse-proxy"],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/posts\/2781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/comments?post=2781"}],"version-history":[{"count":0,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/posts\/2781\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/media?parent=2781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/categories?post=2781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shineservers.com\/wp-json\/wp\/v2\/tags?post=2781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}