当前位置首页 > Nginx知识

nginx动静态分离以及配置https(安全组强行切换以及导致的问题解决)

阅读次数:69 次  来源:admin  发布时间:

公司原来的网络采用http/https同时支持的方式,http并不会强制自动跳转到https,最近要求强制切换,导致了一系列问题。趁今天测试完成了,整理如下:

1、要求HTTP自动跳转到HTTPS;

2、前后端分离;

3、动态的跳转到后端的tomcat(又经过了nginx);

4、前端请求全部通过ajax调用后端服务;

5、只允许GET/POST请求,不允许OPTIONS请求。

首先要编译nginx的时候支持https。

2、配置tomcat 7.0支持cors,如下:

    <filter>
      <filter-name>CorsFilter</filter-name>
      <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
      <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>*</param-value>
      </init-param>
      <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
      </init-param>
      <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
      </init-param>
      <init-param>
        <param-name>cors.exposed.headers</param-name>
        <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
      </init-param>
      <init-param>
        <param-name>cors.support.credentials</param-name>
        <param-value>true</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>CorsFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

3、配置nginx自动重定向到https。

    server {
        listen       80;
        server_name  testhttps.com;
        rewrite ^(.*)$  https://$host$1 permanent;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/ftpuser;
            index  index.html index.htm;

            limit_except GET POST {
                deny  all;
            }
        }

server {
    listen       443;
    server_name  testhttps.com;
    ssl                  on;
    ssl_certificate      /usr/local/nginx/conf/ssl/server.crt;
    ssl_certificate_key  /usr/local/nginx/conf/ssl/server.key;
    
    ssl_session_timeout  5m;
    
#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers   on;
#   
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
        root   /home/ftpuser;   
        limit_except GET POST {
            deny  all;
        }   
    }   
    
     location / {
         #root   html;
         #index  testssl.html index.html index.htm;
         proxy_redirect off;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http://10.7.28.77:8080/transfarNet/;
         limit_except GET POST {
            deny  all;
         }  
     }   
}   

遇到的问题:

1、403问题。我们遇到两个403问题,第一个OPTIONS请求被拦截。第二是有些请求采用POST请求也遇到403,但是GET就不会。

关于OPTIONS,其调用场景如下:

当post请求的 content-type不是one of the “application/x-www-form-urlencoded, multipart/form-data, or text/plain”, 所以Preflighted requests被发起。 “preflighted” requests first send an HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send. 然后得到服务器response许可之后, res.set(‘Access-Control-Allow-Origin’, ‘http://127.0.0.1:3000’); res.set(‘Access-Control-Allow-Methods’, ‘GET, POST, OPTIONS’); res.set(‘Access-Control-Allow-Headers’, ‘X-Requested-With, Content-Type’); 再发起其post请求。

https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS

https://www.w3.org/TR/2014/REC-cors-20140116/

第二个问题,有些请求采用POST请求也遇到403,但是GET就不会。经过仔细排查,是由于ajax请求中没有设置content-Type所致,设置了contentType: "application/x-www-form-urlencoded; charset=utf-8"后,访问就正常了。

上一篇:IIS7开启windows集成权限验证
下一篇:Linux添加/删除用户和用户组