加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
nginx.conf 2.71 KB
一键复制 编辑 原始数据 按行查看 历史
yuebin 提交于 2022-06-25 23:09 . 初始化工程
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8888;
server_name localhost;
location /api/ {
#$http_origin动态获取请求客户端请求的域
# 这个必须有,否则跨域失败
add_header 'Access-Control-Allow-Origin' $http_origin;
# 可选,允许客户端的请求方法
add_header 'Access-Control-Allow-Methods' '*';
# 可选,允许客户端提交Cookie
add_header 'Access-Control-Allow-Credentials' 'true';
# 可选,允许客户端访问的响应头
add_header 'Access-Control-Allow-Headers' 'Cache-Control,Content-Language,token,level,Content-Type,Expires,Last-Modified,Pragma,api-format-tag,Authorization';
# 处理预检请求
if ($request_method = 'OPTIONS') {
# 必须有,指定允许跨域的方法,*代表所有
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' '*';
# 可选,允许客户端访问的响应头
add_header 'Access-Control-Allow-Headers' 'Cache-Control, Content-Language,token,level,Content-Type,Expires,Last-Modified,Pragma,api-format-tag,Authorization';
# 预检请求缓存时间
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
# SpringBoot 应用访问路径
proxy_pass http://localhost:39201/api/;
# proxy_pass http://localhost:10080/;
proxy_set_header Host $host:$server_port; #这一行就是来解决问题的,将NGINX接收到请求头中的Host和端口继续往下传递
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 6000;
proxy_read_timeout 6000;
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化