鍍金池/ 問(wèn)答/Linux  HTML/ nginx 解決跨域問(wèn)題

nginx 解決跨域問(wèn)題

使用 nginx 如何解決跨域問(wèn)題,接口是已 .do 結(jié)尾的,如何通過(guò) nginx 解決跨域

 server {
        listen       80;
        server_name  localhost;
        location ~* (\.do)$ {
            // 這里該怎么寫(xiě)呢?
        }
 }
回答
編輯回答
青黛色

跨域問(wèn)題不應(yīng)該在nginx解決,而且粗暴的添加set_header反而會(huì)出問(wèn)題,nginx并不會(huì)對(duì)這些set_header進(jìn)行驗(yàn)證,反而會(huì)出現(xiàn)一些容易造成瀏覽器混淆的問(wèn)題。

跨域問(wèn)題必須在后臺(tái)解決,而后端框架一定能處理跨域問(wèn)題。讓開(kāi)發(fā)改代碼吧

2017年12月3日 17:48
編輯回答
氕氘氚
 server {
        listen       80;
        server_name  localhost;
        location ~* (\.do)$ {
           add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS';
            add_header Access-Control-Allow-Credentials 'true';
            add_header 'Access-Control-Allow-Headers' 'Accept, Authorization,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            if ($request_method = 'OPTIONS') {
               add_header 'Access-Control-Allow-Origin' *;
               add_header 'Access-Control-Max-Age' 1728000;
               add_header 'Access-Control-Allow-Credentials' 'true';
               add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PUT, OPTIONS';
               add_header 'Access-Control-Allow-Headers' 'Accept, Authorization,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
               add_header 'Content-Type' 'text/plain charset=UTF-8';
               add_header 'Content-Length' 0;
               return 204;
           }
        }
 }
2018年3月4日 16:45
編輯回答
悶騷型
server {
    listen       80;
    server_name  localhost;
    location ~* (\.do)$ {
        proxy_set_header Host $host;
           add_header 'Access-Control-Allow-Origin' '*';
       add_header 'Access-Control-Allow-Credentials' 'true';
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 }
2017年6月8日 07:31