1. 程式人生 > >nginx裏配置跨域

nginx裏配置跨域

接口 accept PC 反向代理 ply frs alt 收藏 sport

發布於 881天前 作者 wendal 1404 次瀏覽 復制 上一個帖子 下一個帖子 標簽: nginx 跨域
    if ($request_method = OPTIONS ) {
        add_header Access-Control-Allow-Origin "*";
        add_header Access-Control-Allow-Methods "GET, OPTIONS, POST, GET";
        add_header Access-Control-Allow-Headers $http_access_control_request_headers;
        add_header Access-Control-Allow-Credentials "true";
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        return 200;
    }
    if ($request_method != OPTIONS ) {
        add_header Access-Control-Allow-Origin "*";
        add_header Access-Control-Allow-Credentials "true";
    }

14 回復 技術分享圖片 Rekoe 技術分享圖片 1樓•881天前

yes


sunhai1988 技術分享圖片 2樓•592天前

我配置nutzwk open api的時候 遇到這個問題。如果使用了反向代理怎麽處理呢?

No ‘Access-Control-Allow-Origin‘ header is present on the requested resource. Origin ‘http://192.168.40.231:8080‘ is therefore not allowed access.
原配置


location / { proxy_pass http://nutz; 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_set_header Upgrade $http_upgrade; proxy_set_header Connection $http_connection; } } upstream nutz{ server 127.0.0.1:8080; keepalive 65; }

wendal 技術分享圖片 3樓•592天前

訪問的是8080?? 不是nginx的80?


sunhai1988 技術分享圖片 4樓•592天前

訪問的是 80 反向代理到 8080


sunhai1988 技術分享圖片 5樓•592天前

訪問的 443


sunhai1988 技術分享圖片 6樓•592天前

走https


wendal 技術分享圖片 7樓•592天前

但這裏報的是8080呢, Chrome控制臺看看請求到哪裏去了


sunhai1988 技術分享圖片 8樓•592天前

8080是我本機開發環境。

我在開發環境一個項目中,調用線上的api接口,然後提示線上的服務,禁止跨域訪問


wendal 技術分享圖片 9樓•592天前

那,我寫的那段代碼,你貼在nginx.conf的什麽地方了?


sunhai1988 技術分享圖片 10樓•592天前

@wendal 在location /{} 裏面

來自炫酷的 NutzCN


wendal 技術分享圖片 11樓•592天前

執行一下curl,貼一下頭部的輸出

curl -v http://xxx.x.x..x.x/yyyyy


sunhai1988 技術分享圖片 12樓•590天前

curl -v https://***.***.com/

> GET / HTTP/1.1
> Host: **.***.com
> User-Agent: curl/7.50.3
> Accept: */*
> 
< HTTP/1.1 302 Found
< Server: nginx
< Date: Mon, 31 Oct 2016 01:10:56 GMT
< Content-Length: 0
< Connection: keep-alive
< X-Powered-By: nutz/1.r.58 <nutzam.com>
< Location: http://***.****.com/sysadmin
< Strict-Transport-Security: max-age=15768000

sunhai1988 技術分享圖片 13樓•589天前

我用過濾器的方式解決了。。有時間在配置nginx吧

GlobalsSettingProcessor

ac.getResponse().addHeader("Access-Control-Allow-Origin", "*");

ecoolper(crskyp) 技術分享圖片 14樓•302天前

收藏了

    if ($request_method = OPTIONS ) {
        add_header Access-Control-Allow-Origin "*";
        add_header Access-Control-Allow-Methods "GET, OPTIONS, POST, GET";
        add_header Access-Control-Allow-Headers $http_access_control_request_headers;
        add_header Access-Control-Allow-Credentials "true";
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        return 200;
    }
    if ($request_method != OPTIONS ) {
        add_header Access-Control-Allow-Origin "*";
        add_header Access-Control-Allow-Credentials "true";
    }

nginx裏配置跨域