Installation of Nginx download, unzip
Download the installation package from Nginx, I am under nginx-1.8.0.tar.gz . The extracted directories are:
[[email protected] third_package]# tar -zxf nginx-1.8.0.tar.gz [[email protected] third_package]# ll nginx-1.8.0total 652drwxr-xr-x 6 1001 1001 4096 Jul 23 18:17 auto-rw-r--r-- 1 1001 1001 249124 Apr 21 2015 CHANGES-rw-r--r-- 1 1001 1001 379021 Apr 21 2015 CHANGES.rudrwxr-xr-x 2 1001 1001 4096 Jul 23 18:17 conf-rwxr-xr-x 1 1001 1001 2478 Apr 21 2015 configuredrwxr-xr-x 4 1001 1001 4096 Jul 23 18:17 contribdrwxr-xr-x 2 1001 1001 4096 Jul 23 18:17 html-rw-r--r-- 1 1001 1001 1397 Apr 21 2015 LICENSEdrwxr-xr-x 2 1001 1001 4096 Jul 23 18:17 man-rw-r--r-- 1 1001 1001 49 Apr 21 2015 READMEdrwxr-xr-x 8 1001 1001 4096 Jul 23 18:17 src
Software that relies on
Before installing the dependent software installed, I use YUM :yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
Installation
[[email protected] nginx-1.8.0]# pwd/installation_package/nginx-1.8.0[[email protected] nginx-1.8.0]# [[email protected] nginx-1.8.0]# ./configure --prefix=/opt/nginx_1
./configureafter execution, a directory is added objs that --prefix represents the installation to this directory if the default installation is not set to /usr/local/nginx .
Compile work:
makemake install
Start
With /opt/nginx_1/sbin/nginx startup, the default is to use the installation directory NGINX_HOME/conf/nginx.conf , that is /opt/nginx_1/conf/nginx.conf .
Of course, you can also use the /opt/nginx_1/sbin/nginx -c /opt/nginx_1/conf/nginx.conf specified configuration file.
Nginx's Reverse proxy
We often use Nginx to do reverse proxy, before setting up the reverse proxy, should first understand the next 正向代理 and 反向代理 .
How to Set
The request to the Nginx will be forwarded to the backend specific host, which can be set 上游服务器 and 代理转发 . Like what:
http { ... upstream myweb { server 127.0.0.1:9999; } server { ... location /myweb { proxy_pass http://myweb; } }}
After setting up, the upstream server is 127.0.0.1:9999 also deployed, you can enjoy the upstream server specific services through Nginx.
But notice the forwarding of the requested information, such as the backend is a tomcat, which runs a servet printing parameters:
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { StringBuffer sb = new StringBuffer (); Sb.append ("Request.getprotocol ():" + Request.getprotocol ()). Append ("\ n"); Sb.append ("Request.getscheme ():" + Request.getscheme ()). Append ("\ n"); Sb.append ("Request.getremoteaddr ():" + request.getremoteaddr ()). Append ("\ n"); Sb.append ("Request.getremotehost ():" + request.getremotehost ()). Append ("\ n"); Sb.append ("Request.getserverport ():" + Request.getserverport ()). Append ("\ n"); Sb.append ("Request.getremoteport ():" + Request.getremoteport ()). Append ("\ n"); Sb.append ("request.getquerystring ():" + request.getquerystring ()). Append ("\ n"); Sb.append ("Request.getremoteuser ():" + Request.getremoteuser ()). Append ("\ n"); Sb.append ("Request.getmethod ():" + Request.getmethod ()). Append ("\ n"); Sb.append ("Request.getlocaladdr ():" + request.getlocaladdr (). Append ("\ n"); Sb.append ("Request.getlocalname ():" + request.getlocalname ()). Append ("\ n"); Sb.append ("Request.getpathinfo ():" + Request.getpathinfo ()). Append ("\ n"); Sb.append ("Request.getrequesturi ():" + Request.getrequesturi ()). Append ("\ n"); Sb.append ("Request.getrequesturl ():" + Request.getrequesturl ()). Append ("\ n"); Sb.append ("Request.getcontextpath ():" + Request.getcontextpath ()). Append ("\ n"); Response.getwriter (). Append ("Served at:"). Append (Request.getcontextpath ()). Append ("\ n"). Append (SB); }
Direct access to Tomcat, the http://nick-huang.com:9999/myweb/PrintEnvInfoServlet?keyword=hello-world printed information is this:
Served at: /mywebrequest.getProtocol() : HTTP/1.1request.getScheme() : httprequest.getRemoteAddr() : 客户端IPrequest.getRemoteHost() : 客户端IPrequest.getServerPort() : 9999request.getRemotePort() : 64494request.getQueryString() : keyword=hello-worldrequest.getRemoteUser() : nullrequest.getMethod() : GETrequest.getLocalAddr() : 服务端IPrequest.getLocalName() : 服务端IPrequest.getPathInfo() : nullrequest.getRequestURI() : /myweb/PrintEnvInfoServletrequest.getRequestURL() : http://nick-huang.com:9999/myweb/PrintEnvInfoServletrequest.getContextPath() : /myweb
Only for the reverse proxy settings, Access Nginx, https://nick-huang.com:777/myweb/PrintEnvInfoServlet?keyword=hello-world after printing:
Served at: /mywebrequest.getProtocol() : HTTP/1.0request.getScheme() : httprequest.getRemoteAddr() : 127.0.0.1request.getRemoteHost() : 127.0.0.1request.getServerPort() : 80request.getRemotePort() : 54856request.getQueryString() : keyword=hello-worldrequest.getRemoteUser() : nullrequest.getMethod() : GETrequest.getLocalAddr() : 127.0.0.1request.getLocalName() : localhostrequest.getPathInfo() : nullrequest.getRequestURI() : /myweb/PrintEnvInfoServletrequest.getRequestURL() : http://myweb/myweb/PrintEnvInfoServletrequest.getContextPath() : /myweb
Request header information delivery after a reverse proxy
It can be found that after the reverse proxy,, and Protocol RemoteAddr ServerPort RequestURL Other parameters are different, then we need to set the agent when the parameters are passed.
Nginx Configuration:
upstream myweb { server 127.0.0.1:9999; keepalive 32; } ... location /myweb { proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass http://myweb; }
For more information, please click on the link: proxy_http_version, keepalive.
Under the /conf/server.xml Tomcat Host node, add the following:
<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https" httpsServerPort="777" />
For more information, please click on the link: org.apache.catalina.valves Class remoteipvalve
Access https://nick-huang.com:777/myweb/PrintEnvInfoServlet?keyword=hello-world , the log is this:
Served at: /mywebrequest.getProtocol() : HTTP/1.1request.getScheme() : httpsrequest.getRemoteAddr() : 客户端IPrequest.getRemoteHost() : 客户端IPrequest.getServerPort() : 777request.getRemotePort() : 55022request.getQueryString() : keyword=hello-worldrequest.getRemoteUser() : nullrequest.getMethod() : GETrequest.getLocalAddr() : 127.0.0.1request.getLocalName() : localhostrequest.getPathInfo() : nullrequest.getRequestURI() : /myweb/PrintEnvInfoServletrequest.getRequestURL() : https://nick-huang.com:777/myweb/PrintEnvInfoServletrequest.getContextPath() : /myweb
"Nginx" Nginx Introduction to Linux