nginx跨域处理

http://www.nginx.cn/nginx-download

nginx.conf配置

if ($request_method = ‘OPTIONS’) { 
        add_header Access-Control-Allow-Origin *; 
        add_header Access-Control-Allow-Credentials true; 
        add_header Access-Control-Allow-Methods ‘GET, POST, OPTIONS’; 
        add_header ‘Access-Control-Allow-Headers’ ‘DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type’; 
        return 200; 
    } 
     if ($request_method = ‘POST’) {

        add_header ‘Access-Control-Allow-Origin’ *; 
        add_header ‘Access-Control-Allow-Credentials’ ‘true’; 
        add_header ‘Access-Control-Allow-Methods’ ‘GET, POST, OPTIONS’; 
        add_header ‘Access-Control-Allow-Headers’ ‘DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type’;

     }

     if ($request_method = ‘GET’) {

        add_header ‘Access-Control-Allow-Origin’ *; 
        add_header ‘Access-Control-Allow-Credentials’ ‘true’; 
        add_header ‘Access-Control-Allow-Methods’ ‘GET, POST, OPTIONS’; 
        add_header ‘Access-Control-Allow-Headers’ ‘DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type’;

     }

 

 

 

user nobody nobody;

worker_processes 1;

error_log logs/error.log;

events {
worker_connections 1024;
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

#访问地址
upstream localhost {

#设定负载均衡的服务器列表
server 192.168.1.151:9001;
}

#设定虚拟主机,默认为监听80端口
server {

listen 80;
server_name localhost;
charset utf-8;

location / {

if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' 'http://192.168.1.41';
#
# Om nom nom cookies
#
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' 'http://192.168.1.41';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' 'http://192.168.1.41';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Metshods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
proxy_pass http://localhost;
}
}

}

 

原文地址:https://www.cnblogs.com/adolfmc/p/5146149.html