nginx主要有两个作用:
- 作为负载均衡服务器,可以将请求转发到后台多个Tomcat服务器;
- 可以专门处理静态页面,动态的JSP等放在Tomcat中处理;
Win下的Nginx
启动:下载后解压压缩包,然后直接双击nginx.exe;此时不会出现任何提示,实际上已经在后台启动了。
通过以下 命令可以查看启动的实例:
tasklist /fi "imagename eq nginx.exe"
结果:
映像名称 PID 会话名 会话# 内存使用
========================= ======== ================ =========== ============
nginx.exe 3148 Console 1 22,256 K
nginx.exe 14380 Console 1 22,568 K
其中一个是主进程,一个是工作进程
停止 :
nginx -s stop:强制退出
nginx -s quit:平缓退出
nginx的配置都是放在conf/nginx.conf文件中进行配置的。具体配置项意义可以参考:https://lufficc.com/blog/configure-nginx-as-a-web-server
下面来看以下两种常见场景:
-
动静分离,后端采用JSP的方式,同时对于/login的请求需要转发到JSP上去;
此时如果location / 指向Nginx本地的话,这种请求就转换不过去,因此需要做如下配置:
location ~ \.(css|png|jpg|html|js)$ { root fh; expires 30d; } location / { proxy_pass http://localhost:8080; index index.jsp; proxy_redirect : :; }
-
启用HTTPS后的动静分离, 配置一个Server即可:
server {
listen 443 ssl;
server_name localhost;
ssl_certificate root.pem;
ssl_certificate_key rootkey.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location ~ \.(css|png|jpg|html|js)$ {
root fh;
expires 30d;
}
location / {
proxy_pass http://localhost:8080;
index index.jsp;
proxy_redirect : :;
}
}