大佬教程收集整理的这篇文章主要介绍了Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1. 正向代理,意思是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。
访问google使用代理服务器
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/b39571c14df326025e44f7f43b7cdb7a.png" >
2. 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器
代理访问自己的内部服务器
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/67d8e2eba26dd7a67873edff0fb62996.png" >
@H_197_22@二、Nginx工作原理Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个LOCATIOn block(LOCATIOn是Nginx配置中的一个指令,用于URL匹配),而在这个LOCATIOn中所配置的每个指令将会启动不同的模块去完成相应的工作。
Nginx的模块从结构上分为核心模块、基础模块和第三方模块:核心模块:http模块、EVENT模块和MAIL模块基础模块:http Access模块、http FastCGI模块、http Proxy模块和http Rewrite模块,第三方模块:http Upstream request Hash模块、Notice模块和http Access Key模块。
Nginx的高并发得益于其采用了epoll模型,与传统的服务器程序架构不同,epoll是linux内核2.6以后才出现的。Nginx采用epoll模型,异步非阻塞,而Apache采用的是SELEct模型: SELEct特点:SELEct 选择句柄的时候,是遍历所有句柄,也就是说句柄有事件响应时,SELEct需要遍历所有句柄才能获取到哪些句柄有事件通知,因此效率是非常低。 epoll的特点:epoll对于句柄事件的选择不是遍历的,是事件响应的,就是句柄上事件来就马上选择出来,不需要遍历整个句柄链表,因此效率非常高。
@H_197_22@1. 什么是Nginxnginx ("ENGIne x") 是一个高性能的http和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名
常见的应用服务器: Apache/Microsoft IIS/tomcat/Lighttpd/Nginx
@H_197_22@2. Nginx的特点和组成特点:Nginx特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好
组成:Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单
核心模块:http模块、EVENT模块和MAIL模块 基础模块:http Access模块、http FastCGI模块、http Proxy模块和http Rewrite模块, 第三方模块:http Upstream request Hash模块、Notice模块和http Access Key模块。
@H_197_22@3. Nginx的优点1)高并发响应性能非常好,官方Nginx处理静态文件并发5w/s2)反向代理性能非常强。(可用于负载均衡)3)内存和cpu占用率低。(为Apache的1/5-1/10)4)对后端服务有健康检查功能。5)支持php cgi方式和fastcgi方式。6)配置代码简洁且容易上手。
@H_197_22@4. Nginx安装
检查系统里是否安装了pcre软件
rpm -qa pcre 如果没有显示说明没有安装 反之安装过
rpm -e --nodeps pcre 删除pcre
安装pcre:yum install pcre-devel pcre -y
#下载Nginx源码包
cd /usr/localwget http://nginx.org/download/nginx-1.9.0.tar.gz
#解压Nginx源码包
tar -zxvf nginx-1.9.0.tar.gz
#进入解压目录,开始安装,默认安装地址 /usr/local/nginx
cd nginx-1.9.0./configure --prefix=/usr/local/nginx --with-http_ssl_module &&make &&make install
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/f736a40f3e28e0fde2f58cdd5efeecfe.png" >
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/afe0eeaf94faeef5baea0e50b64be499.png" >
#自此Nginx安装完毕检查nginx配置文件是否正确,返回OK即正确。/usr/local/nginx/sbin/nginx -t
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/2c16e532381004edd07f1f7e92ca1f06.png" >
#然后启动nginx,/usr/local/nginx/sbin/nginx 回车即可。#查看进程是否已启动:ps aux |grep nginx
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/00fbc67bb7d4e1e085ebeb849112f5d5.png" >
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/ac2b9d1b84f54a34c6046b7faab2a21f.png" >
删除nginx文件即可
rm -rf /usr/local/nginx
查看nginx进程:ps -ef|grep nginx或者ps aux |grep nginx说明:nginx的进程由主进程和工作进程组成。
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/57c247ca259a1dc2fe486ae3ef926370.png" >
启动nginx:/usr/local/nginx/sbin/nginx说明:启动结果显示nginx的主线程和工作线程,工作线程的数量跟nginx.conf中的配置参数worker_processes有关。
平滑启动nginx:kill -HUP 'cat /var/run/nginx.pid' 或者/usr/local/nginx/sbin/nginx -s reload其中进程文件路径在配置文件nginx.conf中可以找到。平滑启动的意思是在不停止nginx的情况下,重启nginx,重新加载配置文件,启动新的工作线程,完美停止旧的工作线程。
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/eaa28c42f6efc0fc5c3f89791adc64e9.png" >
完美停止nginx:kill -QUIT 'cat /var/run/nginx.pid'
快速停止nginx :kill -TERM 'cat /var/run/nginx.pid'或者kill -INT `cat /var/run/nginx.pid`完美停止工作进程(主要用于平滑升级):kill -WINCH 'cat /var/run/nginx.pid'
强制停止nginx:pkill -9 nginx
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/c79171b29b2222ec522c1c0cc7091669.png" >
检查对nginx.conf文件的修改是否正确:nginx -t -c /etc/nginx/nginx.conf 或者 /usr/local/nginx/sbin/nginx -t
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/4f8c68f1ecbcc874b70f35e657a01be3.png" >
停止nginx的命令:/usr/local/nginx/sbin/nginx -s stop
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/7ec4aea0d987f308e9df95cd413e2246.png" >
查看nginx的版本信息:/usr/local/nginx/sbin/nginx -v
查看完整的nginx的配置信息:/usr/local/nginx/sbin/nginx -V
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/ed9eae55e10a824dbff99f40119888d0.png" >
@H_197_22@2. Nginx升级下载所需版本的Nginx:wget http://www.nginx.org/download/nginx-1.4.2.tar.gz
获取旧版本nginx的configure选项:/usr/local/nginx/sbin/nginx -V 编译新版本的nginxtar -xvf nginx-1.4.2.tar.gz cd nginx-1.4.2 ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module make
备份旧版本的nginx可执行文件,复制新版本的nginx可执行文件:mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old cp objs/nginx /usr/local/nginx/sbin/
测试新版本nginx是否正常:/usr/local/nginx/sbin/nginx -t
平滑重启升级nginx:kill –USR2 `cat /usr/local/nginx/log/nginx.pid` 旧版本Nginx的pid变为oldbin,这是旧版本和新版本的nginx同时运行,过一段时间等旧nginx处理完用户请求后,执行下面操作从容关闭旧版本的Nginx进程kill -WINCH `cat /usr/local/nginx/log/nginx.oldbin`
决定是否升级到新版的nginx:kill –HUP `cat /usr/local/nginx/log/nginx.oldbin` ##nginx在不重载配置文件启动工作进程 kill –QUIT `cat /usr/local/nginx/log/nginx.oldbin` ##关闭旧版nginx 验证nginx是否升级成功/usr/local/nginx/sbin/nginx –V ###显示下图则升级成功
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/4618f2faf554e1745230e05aac619caf.png" >
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/691004dc55cba45b279e3083e4702265.png" >
说明:
在Nginx的nginx.conf里面监听80端口,并配置后端tomcat的地址,前端访问的时候即可转发到后端的tomcat
@H_197_22@2. 搭建步骤title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/d2b3accd406c5bc26e38d628329a092a.png" >
8080的server.xml文件
<span style="color: #008000">
<span style="color: #008000">
<span style="color: #008000"><!--</span><span style="color: #008000">The connectors can use a shared executor,you can define one or more named thread pools</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000">
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java http Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (http/AJp) Connector: /docs/aPR.html
Define a non-SSL http/1.1 Connector on port 8080
</span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">Connector </span><span style="color: #ff0000">port</span><span style="color: #0000ff">="8080"</span><span style="color: #ff0000"> protocol</span><span style="color: #0000ff">="http/1.1"</span><span style="color: #ff0000">
connectionTimeout</span><span style="color: #0000ff">="20000"</span><span style="color: #ff0000">
redirectPort</span><span style="color: #0000ff">="8443"</span> <span style="color: #0000ff">/></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> A "Connector" using the shared thread pool</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000">
<Connector executor="tomcatThreadPool"
port="8080" protocol="http/1.1"
connectionTimeout="20000"
redirectPort="8443" />
</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> Define a SSL http/1.1 Connector on port 8443
This connector uses the BIO implementation that requires the JSSE
style configuration. When using the APR/native implementation,the
OpenSSL style configuration is required as described in the APR/native
documentation </span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000">
<Connector port="8443" protocol="org.apache.coyote.http11.http11Protocol"
maxThreads="150" SSLEnabled="true" scheR_409_11845@e="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> Define an AJP 1.3 Connector on port 8009 </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">Connector </span><span style="color: #ff0000">port</span><span style="color: #0000ff">="8009"</span><span style="color: #ff0000"> protocol</span><span style="color: #0000ff">="AJP/1.3"</span><span style="color: #ff0000"> redirectPort</span><span style="color: #0000ff">="8443"</span> <span style="color: #0000ff">/></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> An ENGIne represents the entry point (within Catalina) that processes
every request. The ENGIne implementation for tomcat stand alone
analyzes the http headers included with the request,and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/ENGIne.html </span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> You should set jvmRoute to support load-balancing via AJP ie :
<ENGIne name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
</span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">ENGIne </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="Catalina"</span><span style="color: #ff0000"> defaultHost</span><span style="color: #0000ff">="localhost"</span><span style="color: #0000ff">></span>
<span style="color: #008000"><!--</span><span style="color: #008000">For clustering,please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) </span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">Realm </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.realm.LockOutRealm"</span><span style="color: #0000ff">></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">Realm </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.realm.UserDatabaseRealm"</span><span style="color: #ff0000">
resourcename</span><span style="color: #0000ff">="UserDatabase"</span><span style="color: #0000ff">/></span>
<span style="color: #0000ff"></</span><span style="color: #800000">Realm</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"><</span><span style="color: #800000">Host </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="localhost"</span><span style="color: #ff0000"> appBase</span><span style="color: #0000ff">="webapps"</span><span style="color: #ff0000">
unpackWARs</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> autoDeploy</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> SingleSignOn valve,share authentication between web applications
Documentation at: /docs/config/valve.html </span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000">
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">Valve </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.valves.AccessLogValve"</span><span style="color: #ff0000"> directory</span><span style="color: #0000ff">="logs"</span><span style="color: #ff0000">
prefix</span><span style="color: #0000ff">="localhost_access_log."</span><span style="color: #ff0000"> suffix</span><span style="color: #0000ff">=".txt"</span><span style="color: #ff0000">
pattern</span><span style="color: #0000ff">="%h %l %u %t &quot;%r&quot; %s %b"</span> <span style="color: #0000ff">/></span>
<span style="color: #0000ff"></</span><span style="color: #800000">Host</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">ENGIne</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></
<span style="color: #800000">service<span style="color: #0000ff">>8081的server.xml文件
<span style="color: #008000">
<span style="color: #008000">
<span style="color: #008000"><!--</span><span style="color: #008000"> A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java http Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (http/AJp) Connector: /docs/aPR.html
Define a non-SSL http/1.1 Connector on port 8080
</span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">Connector </span><span style="color: #ff0000">port</span><span style="color: #0000ff">="8081"</span><span style="color: #ff0000"> protocol</span><span style="color: #0000ff">="http/1.1"</span><span style="color: #ff0000">
connectionTimeout</span><span style="color: #0000ff">="20000"</span><span style="color: #ff0000">
redirectPort</span><span style="color: #0000ff">="8443"</span> <span style="color: #0000ff">/></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> A "Connector" using the shared thread pool</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000">
<Connector executor="tomcatThreadPool"
port="8080" protocol="http/1.1"
connectionTimeout="20000"
redirectPort="8443" />
</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> Define a SSL http/1.1 Connector on port 8443
This connector uses the BIO implementation that requires the JSSE
style configuration. When using the APR/native implementation,the
OpenSSL style configuration is required as described in the APR/native
documentation </span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000">
<Connector port="8443" protocol="org.apache.coyote.http11.http11Protocol"
maxThreads="150" SSLEnabled="true" scheR_409_11845@e="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> Define an AJP 1.3 Connector on port 8009 </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">Connector </span><span style="color: #ff0000">port</span><span style="color: #0000ff">="8010"</span><span style="color: #ff0000"> protocol</span><span style="color: #0000ff">="AJP/1.3"</span><span style="color: #ff0000"> redirectPort</span><span style="color: #0000ff">="8443"</span> <span style="color: #0000ff">/></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> An ENGIne represents the entry point (within Catalina) that processes
every request. The ENGIne implementation for tomcat stand alone
analyzes the http headers included with the request,share authentication between web applications
Documentation at: /docs/config/valve.html </span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000">
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
</span><span style="color: #008000">--></span>
<span style="color: #008000"><!--</span><span style="color: #008000"> Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" </span><span style="color: #008000">--></span>
<span style="color: #0000ff"><</span><span style="color: #800000">Valve </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.valves.AccessLogValve"</span><span style="color: #ff0000"> directory</span><span style="color: #0000ff">="logs"</span><span style="color: #ff0000">
prefix</span><span style="color: #0000ff">="localhost_access_log."</span><span style="color: #ff0000"> suffix</span><span style="color: #0000ff">=".txt"</span><span style="color: #ff0000">
pattern</span><span style="color: #0000ff">="%h %l %u %t &quot;%r&quot; %s %b"</span> <span style="color: #0000ff">/></span>
<span style="color: #0000ff"></</span><span style="color: #800000">Host</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">ENGIne</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></
<span style="color: #800000">service<span style="color: #0000ff">>tomcat-8080/webapps/ROOT
欢迎来到8080端口tomcat
<span style="color: #0000ff"></<span style="color: #800000">body<span style="color: #0000ff">>tomcat-8081/webapps/ROOT
欢迎来到8081端口tomcat
<span style="color: #0000ff"></<span style="color: #800000">body<span style="color: #0000ff">>events
{
include mime.types;
default_type application
client_header_buffer_size 4k;
large_client_header_buffers <span style="color: #800000; font-weight: bold">4<span style="color: #000000"> 32k;
client_max_body_size 300m; #允许客户端请求的最大单文件字节数 上传文件时根据需求设置这个参数
sendfile <span style="color: #0000ff">on<span style="color: #000000">;
tcp_nopush <span style="color: #0000ff">on<span style="color: #000000">; #防止网络阻塞
keepalive_timeout <span style="color: #800000; font-weight: bold">60<span style="color: #000000">;
tcp_nodelay <span style="color: #0000ff">on<span style="color: #000000">; #提高数据的实时响应性
clienT_Body_buffer_size 512k; #缓冲区代理缓冲用户端请求的最大字节数(请求多)
proxy_connect_timeout <span style="color: #800000; font-weight: bold">5<span style="color: #000000">; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout <span style="color: #800000; font-weight: bold">60<span style="color: #000000">; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_send_timeout <span style="color: #800000; font-weight: bold">5<span style="color: #000000">; #后端服务器数据回传时间(代理发送超时)
proxy_buffer_size 16k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers <span style="color: #800000; font-weight: bold">4<span style="color: #000000"> 64k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 128k; #高负荷下缓冲大小
proxy_temp_file_write_size 128k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
gzip <span style="color: #0000ff">on<span style="color: #000000">; #NGINX可以压缩静态资源,比如我的静态资源有10M,压缩后只有2M,那么浏览器下载的就少了
gzip_min_length 1k;
gzip_buffers <span style="color: #800000; font-weight: bold">4<span style="color: #000000"> 16k;
gzip_http_version <span style="color: #800000; font-weight: bold">1.1<span style="color: #000000">;
gzip_comp_level <span style="color: #800000; font-weight: bold">2<span style="color: #000000">; #压缩级别大小,最小1,最大9.值越小,压缩后比例越小,CPU处理更快,为1时,原10M压缩完后8M,但设为9时,压缩完可能只有2M了。一般设置为2
gzip_types <span style="color: #0000ff">text<span style="color: #808080">/plain application<span style="color: #808080">/x<span style="color: #808080">-javascript <span style="color: #0000ff">text<span style="color: #808080">/css application<span style="color: #808080">/xml; #压缩类型:<span style="color: #0000ff">text<span style="color: #000000">,js css xml 都会被压缩
gzip_vary <span style="color: #0000ff">on<span style="color: #000000">; #作用是在http响应中增加一行目的是改变反向代理服务器的缓存策略
log_format main <span style="color: #ff0000">'<span style="color: #ff0000">$remote_addr - $remote_user [$time_local] "$request" <span style="color: #ff0000">'<span style="color: #000000"> #ip 远程用户 当地时间 请求URL
<span style="color: #ff0000">'<span style="color: #ff0000">$status $body_bytes_sent "$http_referer" <span style="color: #ff0000">'<span style="color: #000000"> #状态 发送的大小 响应的头
<span style="color: #ff0000">'<span style="color: #ff0000">"$http_user_agent" $request_time<span style="color: #ff0000">'<span style="color: #000000">; #客户端使用的浏览器 页面响应的时间
upstream web1 {
#ip_hash;
#转发的后端的tomcat服务器,weight表示转发的权重,越大转发的次数越多,机器性能不一样配置的weight值不一样
server </span><span style="color: #800000; font-weight: bold">127.0</span>.<span style="color: #800000; font-weight: bold">0.1</span>:<span style="color: #800000; font-weight: bold">8080</span> weight<span style="color: #808080">=</span><span style="color: #800000; font-weight: bold">1</span> max_fails<span style="color: #808080">=</span><span style="color: #800000; font-weight: bold">2</span> fail_timeout<span style="color: #808080">=</span><span style="color: #000000">30s;
server </span><span style="color: #800000; font-weight: bold">127.0</span>.<span style="color: #800000; font-weight: bold">0.1</span>:<span style="color: #800000; font-weight: bold">8081</span> weight<span style="color: #808080">=</span><span style="color: #800000; font-weight: bold">1</span> max_fails<span style="color: #808080">=</span><span style="color: #800000; font-weight: bold">2</span> fail_timeout<span style="color: #808080">=</span><span style="color: #000000">30s;
}
upstream web2 {
server <span style="color: #800000; font-weight: bold">127.0.<span style="color: #800000; font-weight: bold">0.1:<span style="color: #800000; font-weight: bold">8090 weight<span style="color: #808080">=<span style="color: #800000; font-weight: bold">1 max_fails<span style="color: #808080">=<span style="color: #800000; font-weight: bold">2 fail_timeout<span style="color: #808080">=<span style="color: #000000">30s;
server <span style="color: #800000; font-weight: bold">127.0.<span style="color: #800000; font-weight: bold">0.1:<span style="color: #800000; font-weight: bold">8091 weight<span style="color: #808080">=<span style="color: #800000; font-weight: bold">1 max_fails<span style="color: #808080">=<span style="color: #800000; font-weight: bold">2 fail_timeout<span style="color: #808080">=<span style="color: #000000">30s;
}
server {
listen <span style="color: #800000; font-weight: bold">80<span style="color: #000000">; #监听80端口
server_name www.dbspread.com; #域名
<span style="color: #0000ff">index <span style="color: #0000ff">index.jsp <span style="color: #0000ff">index.html <span style="color: #0000ff">index<span style="color: #000000">.htm;
root <span style="color: #808080">/usr<span style="color: #808080">/local<span style="color: #808080">/nginx<span style="color: #808080">/<span style="color: #000000">html; #定义服务器的默认网站根目录位置
#监听完成以后通过斜杆(</span><span style="color: #808080">/</span><span style="color: #000000">)拦截请求转发到后端的tomcat服务器
LOCATIOn </span><span style="color: #808080">/</span><span style="color: #000000">
{
#如果后端的服务器返回502、</span><span style="color: #800000; font-weight: bold">504</span><span style="color: #000000">、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现@R_530_10772@。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host; #获取客户端的主机名存到变量Host里面,从而让tomcat取到客户端机器的信息
proxy_set_header X</span><span style="color: #808080">-</span><span style="color: #0000ff">Real</span><span style="color: #808080">-</span>IP $remote_addr; #获取客户端的主机名存到变量X<span style="color: #808080">-</span><span style="color: #0000ff">Real</span><span style="color: #808080">-</span><span style="color: #000000">IP里面,从而让tomcat取到客户端机器的信息
proxy_set_header X</span><span style="color: #808080">-</span>ForWARDed<span style="color: #808080">-</span><span style="color: #0000ff">For</span><span style="color: #000000"> $proxy_add_x_forWARDed_for;
proxy_pass http:</span><span style="color: #808080">//</span><span style="color: #000000">web1; #跳转到对应的应用web1
}
# LOCATIOn </span><span style="color: #808080">~</span> .<span style="color: #808080">*</span>\.(php<span style="color: #808080">|</span>jsp<span style="color: #808080">|</span>cgi<span style="color: #808080">|</span>shtml)?$ #动态分离 <span style="color: #808080">~</span>匹配 以.<span style="color: #808080">*</span><span style="color: #000000">结尾(以php JSP结尾走这段)
# {
# proxy_set_header Host $host;
# proxy_set_header X</span><span style="color: #808080">-</span><span style="color: #0000ff">Real</span><span style="color: #808080">-</span><span style="color: #000000">IP $remote_addr;
# proxy_set_header X</span><span style="color: #808080">-</span>ForWARDed<span style="color: #808080">-</span><span style="color: #0000ff">For</span><span style="color: #000000"> $proxy_add_x_forWARDed_for;
# proxy_pass http:</span><span style="color: #808080">//</span><span style="color: #000000">jvm_web2;
# }
#静态分离 </span><span style="color: #808080">~</span>匹配 以.<span style="color: #808080">*</span><span style="color: #000000">结尾(以php JSP结尾走这段),当然不是越久越好,如果有10000个用户在线,都保存几个月,系统托跨
LOCATIOn </span><span style="color: #808080">~</span> .<span style="color: #808080">*</span>\.(html<span style="color: #808080">|</span>htm<span style="color: #808080">|</span>gif<span style="color: #808080">|</span>jpg<span style="color: #808080">|</span>jpeg<span style="color: #808080">|</span>bmp<span style="color: #808080">|</span>png<span style="color: #808080">|</span>ico<span style="color: #808080">|</span>txt<span style="color: #808080">|</span>js<span style="color: #808080">|</span><span style="color: #000000">css)$
{
root </span><span style="color: #808080">/</span><span style="color: #ff00ff">var</span><span style="color: #808080">/</span>local<span style="color: #808080">/</span><span style="color: #000000">static;
expires 30d;
}
#日志级别有</span><span style="color: #ff0000">[</span><span style="color: #ff0000">debug|info|notice|warn|error|crit</span><span style="color: #ff0000">]</span><span style="color: #000000"> error_log 级别分为 debug,info,notice,warn,error,crit 默认为crit,生产环境用error
#crit 记录的日志最少,而debug记录的日志最多
access_log </span><span style="color: #808080">/</span>usr<span style="color: #808080">/</span>local<span style="color: #808080">/</span>logs<span style="color: #808080">/</span>web2<span style="color: #808080">/</span>access.<span style="color: #ff00ff">log</span><span style="color: #000000"> main;
error_log </span><span style="color: #808080">/</span>usr<span style="color: #808080">/</span>local<span style="color: #808080">/</span>logs<span style="color: #808080">/</span>web2<span style="color: #808080">/</span>error.<span style="color: #ff00ff">log</span><span style="color: #000000"> crit;
}
}
说明:nginx配置文件的解析流程
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/d66145e0429d64c75a0c910114618410.png" >
/usr/local/nginx/sbin/nginx
192.168.152.129 www.dbspread.com
首次进入效果:
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/17894534f3954fb6f84ff7c7c1fd91d2.png" >
再次刷新页面效果
title="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" alt="Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡" src="https://cn.js-code.com/res/2019/02-10/10/da01f667564b3af1237593ff43e3621b.png" >
以上是大佬教程为你收集整理的Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡全部内容,希望文章能够帮你解决Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。