nginx安装启动及动静分离、https配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/icarusliu/article/details/85319482

nginx主要有两个作用:

  • 作为负载均衡服务器,可以将请求转发到后台多个Tomcat服务器;
  • 可以专门处理静态页面,动态的JSP等放在Tomcat中处理;

Win下的Nginx

下载:http://nginx.org

启动:下载后解压压缩包,然后直接双击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

下面来看以下两种常见场景:

  1. 动静分离,后端采用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 : :;
     }
    
  2. 启用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 : :;
		}
    }

猜你喜欢

转载自blog.csdn.net/icarusliu/article/details/85319482