nginx Cors跨域请求OPTIONS方法405 Method Not Allowed问题

使用nginx反向代理一部分api链接。但是遇到了405 Method Not Allowed问题。

仔细观察了一下,发现是Cors跨域请求OPTIONS方法的时候,nginx不允许OPTIONS方法导致的。

解决思路:

1、405返回200码

2、405时,response返回Cors协议允许的Header

配置如下:

server {
        listen       8080;
        server_name  abc.com;
		error_page   405 =200 @405;
        location / {
            root   D:/b/dist;
            index  index.html index.htm;
		}
		location ~ ^/(api)/  {
		    
			proxy_redirect off;
			proxy_set_header Host $http_host;
			proxy_set_header X-Forwarded-Proto $scheme;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header Upgrade $http_upgrade; 
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_connect_timeout 18000;
			proxy_send_timeout 18000;
			proxy_read_timeout 18000;
			proxy_pass http://127.0.0.1:9090;
		}
		location @405{
			add_header Content-Length 0;
			add_header Content-Type text/plain;
			add_header Access-Control-Allow-Headers *;
			add_header Access-Control-Allow-Methods *;
			add_header Access-Control-Allow-Origin *;
			return 200;
		}
}

 

参考文献:

https://blog.csdn.net/u014344668/article/details/54948546

分享到:更多 ()