Nginx的安装
下载、解压
从Nginx下载安装包,我下的是nginx-1.8.0.tar.gz
。解压后的目录为:
[[email protected] third_package]# tar -zxf nginx-1.8.0.tar.gz
[[email protected] third_package]# ll nginx-1.8.0
total 652
drwxr-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.ru
drwxr-xr-x 2 1001 1001 4096 Jul 23 18:17 conf
-rwxr-xr-x 1 1001 1001 2478 Apr 21 2015 configure
drwxr-xr-x 4 1001 1001 4096 Jul 23 18:17 contrib
drwxr-xr-x 2 1001 1001 4096 Jul 23 18:17 html
-rw-r--r-- 1 1001 1001 1397 Apr 21 2015 LICENSE
drwxr-xr-x 2 1001 1001 4096 Jul 23 18:17 man
-rw-r--r-- 1 1001 1001 49 Apr 21 2015 README
drwxr-xr-x 8 1001 1001 4096 Jul 23 18:17 src
依赖的软件
安装之前把依赖的软件装上,我这里用YUM
:yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
安装
[[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
执行./configure
后在添加了一个目录objs
,--prefix
表示安装到此目录,如果不设置默认安装到/usr/local/nginx
。
编译工作:
make
make install
启动
用/opt/nginx_1/sbin/nginx
启动,默认使用的是安装目录的NGINX_HOME/conf/nginx.conf
,也就是/opt/nginx_1/conf/nginx.conf
。
当然,也可以使用/opt/nginx_1/sbin/nginx -c /opt/nginx_1/conf/nginx.conf
指定配置文件。
Nginx的反向代理
我们常用Nginx做反向代理,在设置反向代理前,应先了解下正向代理
和反向代理
。
如何设置
将到达Nginx的请求转到后端具体的主机,可通过设置上游服务器
和代理转发
。比如:
http {
...
upstream myweb {
server 127.0.0.1:9999;
}
server {
...
location /myweb {
proxy_pass http://myweb;
}
}
}
设置好之后,将上游服务器127.0.0.1:9999
也部署好,就可以通过Nginx享受上游服务器的具体服务了。
但要注意请求的信息的转发,比如后端是一台TOMCAT,里面运行一个Servet打印各项参数:
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);
}
直接访问TOMCAT,http://nick-huang.com:9999/myweb/PrintEnvInfoServlet?keyword=hello-world
,打印的信息是这样的:
Served at: /myweb
request.getProtocol() : HTTP/1.1
request.getScheme() : http
request.getRemoteAddr() : 客户端IP
request.getRemoteHost() : 客户端IP
request.getServerPort() : 9999
request.getRemotePort() : 64494
request.getQueryString() : keyword=hello-world
request.getRemoteUser() : null
request.getMethod() : GET
request.getLocalAddr() : 服务端IP
request.getLocalName() : 服务端IP
request.getPathInfo() : null
request.getRequestURI() : /myweb/PrintEnvInfoServlet
request.getRequestURL() : http://nick-huang.com:9999/myweb/PrintEnvInfoServlet
request.getContextPath() : /myweb
只作反向代理的设置,访问NGINX,https://nick-huang.com:777/myweb/PrintEnvInfoServlet?keyword=hello-world
,后打印:
Served at: /myweb
request.getProtocol() : HTTP/1.0
request.getScheme() : http
request.getRemoteAddr() : 127.0.0.1
request.getRemoteHost() : 127.0.0.1
request.getServerPort() : 80
request.getRemotePort() : 54856
request.getQueryString() : keyword=hello-world
request.getRemoteUser() : null
request.getMethod() : GET
request.getLocalAddr() : 127.0.0.1
request.getLocalName() : localhost
request.getPathInfo() : null
request.getRequestURI() : /myweb/PrintEnvInfoServlet
request.getRequestURL() : http://myweb/myweb/PrintEnvInfoServlet
request.getContextPath() : /myweb
反向代理后的请求头信息传递
可以发现,反向代理后Protocol
、RemoteAddr
、ServerPort
、RequestURL
等参数均有所不同,那么我们需要设置代理时传递参数。
Nginx配置:
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;
}
相关说明,请点击链接:proxy_http_version、keepalive。
Tomcat的/conf/server.xml
的Host
节点下添加:
<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https" httpsServerPort="777" />
相关说明,请点击链接:org.apache.catalina.valves Class RemoteIpValve
访问https://nick-huang.com:777/myweb/PrintEnvInfoServlet?keyword=hello-world
,日志是这样的:
Served at: /myweb
request.getProtocol() : HTTP/1.1
request.getScheme() : https
request.getRemoteAddr() : 客户端IP
request.getRemoteHost() : 客户端IP
request.getServerPort() : 777
request.getRemotePort() : 55022
request.getQueryString() : keyword=hello-world
request.getRemoteUser() : null
request.getMethod() : GET
request.getLocalAddr() : 127.0.0.1
request.getLocalName() : localhost
request.getPathInfo() : null
request.getRequestURI() : /myweb/PrintEnvInfoServlet
request.getRequestURL() : https://nick-huang.com:777/myweb/PrintEnvInfoServlet
request.getContextPath() : /myweb
原文地址:https://www.cnblogs.com/nick-huang/p/7231627.html