user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/json;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80 ;
server_name localhost;
# 指定前端项目所在的位置
location / {
root /etc/nginx/monitor;
index index.html index.htm;
# try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /etc/nginx;
}
location ~ /\. {
deny all;
}
# 反向代理到后端
location /api {
default_type application/json;
#internal;
keepalive_timeout 30s;
keepalive_requests 1000;
#支持keep-alive
proxy_http_version 1.1;
rewrite /api(/.*) $1 break;
proxy_pass_request_headers on;
#more_clear_input_headers Accept-Encoding;
proxy_next_upstream error timeout;
proxy_pass http://jdkhouduan:8888;
#proxy_pass http://backend;
# 添加跨域头部
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
# 针对 OPTIONS 请求返回 204 No Content,以支持预检请求(preflight)
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
}
}
# upstream backend {
# server 127.0.0.1:8888 max_fails=5 fail_timeout=10s weight=1;
# #server 127.0.0.1:8082 max_fails=5 fail_timeout=10s weight=1;
# }
}
为什么配置了跨域请求头,还是解决不了跨域问题