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

公司原来的网络采用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"后,访问就正常了。

posted @   zhjh256  阅读(704)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2016-09-21 velocity merge作为工具类从web上下文和jar加载模板的两种常见情形
点击右上角即可分享
微信分享提示