参考了这篇文章:nginx禁止跨域_杰克琼斯D雨的博客-CSDN博客
map $http_origin $cors_origin {
default 1;
"~^(http?://(localhost)(:[0-9]+)?)$" 1;
"~^(http?://(192.168.xx.xx)(:[0-9]+)?)$" 1;
"~*" 0;
}
server {
listen 81;
access_log /var/log/backend.log ;
server_name _;
location / {
include uwsgi_params;
proxy_pass http://127.0.0.1:8000/api/;
error_page 404 /;
}
}
server {
listen 8080;
server_name _;
access_log /var/log/frontend.log ;
location /notification/ {
proxy_pass http://127.0.0.1:8000/notification/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location ^~ /bak/ {
alias /bak/;
if ($request_filename ~* ^.*?\.(des3|doc|pdf|tar\.gz|zip|docx|txt)$) {
add_header Content-Disposition attachment;
add_header Content-Type application/octet-stream;
}
sendfile on;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
charset utf-8,gbk;
}
location ^~/api/ {
proxy_pass http://127.0.0.1:81/;
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_set_header X-NginX-Proxy true;
error_log /var/log/error_api.log;
if ($cors_origin = 0) {
return 403;
}
}
location / {
try_files $uri $uri/ /index.html;
root /www/dist;
index index.html index.htm;
}
}