1.测试反向代理
例如我的Tomcat默认地址为:http://localhost:8080/
n1.png
我的nginx的默认地址为:http://localhost:8011/
Paste_Image.png
注意:
1.成功启动nginx会出现2个进程
Paste_Image.png
启动失败的话,到logs文件夹查看error.log文件,一般启动错误是因为端口被占用,“2017/10/19 21:51:39 [emerg] 8460#8556: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)”错误就是指80端口被占用
2.查看端口占用可以用dos命令“netstat -ano | findstr 80”查看,比如我这个命令是查看80端口,假如没有进程占用端口,则什么消息也不显示
3.我默认80端口为iis用了,所以我这里用的端口修改为8011,启动成功访问会出现上图的欢迎页面
Paste_Image.png
修改nginx的配置文件nginx.conf
Paste_Image.png
访问http://localhost:8011/
Paste_Image.png
2.测试负载均衡
创建多一个tomcat服务器地址:http://localhost:8085/
Paste_Image.png
修改nginx的配置文件nginx.conf
Paste_Image.png
这时候访问http://localhost:8011/就有可能出现tomcat1和tomcat2页面,可以在server后面设置weight,默认为1,weight越大,负载的权重就越大。
Paste_Image.png
Paste_Image.png
3.测试代理静态资源
上面实现反向代理,实际上我们的需求不是这样的,我们需要分文件类型来进行过滤,比如jsp直接给tomcat处理,因为nginx并不是servlet容器,没办法处理JSP,而html,js,css这些不需要处理的,直接给nginx进行缓存。
修改nginx的配置文件nginx.conf
Paste_Image.png
访问jsp页面
Paste_Image.png
随便找一个相应的文件查看对应的server
Paste_Image.png