user nginx; #配置运行 nginx 服务器用户
worker_processes 1; # 配置允许生成的 worker process 数
error_log /var/log/nginx/error.log warn; # 配置错误日志的存放路径
pid /var/run/nginx.pid; # 配置 nginx 进程 PID 存放路径
events { # 与用户的网络连接相关的
worker_connections 1024; # 配置最大连接数
}
http { # 代理,缓存,日志,以及第三方模块的配置
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on; # 配置允许 sendfile 方式传输文件
#tcp_nopush on;
keepalive_timeout 65; # 配置连接超时时间
#gzip on;
include /etc/nginx/conf.d/*.conf; # 配置文件的引入
server {
listen 80;
server_name localhost 127.0.0.1;
location / {
# 允许跨域请求的“域”
add_header 'Access-Control-Allow-Origin' $http_origin;
# 允许客户端提交Cookie
add_header 'Access-Control-Allow-Credentials' 'true';
# 允许客户端的请求方法
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
# 允许客户端提交的的请求头
add_header 'Access-Control-Allow-Headers' 'Origin, x-requested-with, Content-Type, Accept, Authorization';
# 允许客户端访问的响应头
add_header 'Access-Control-Expose-Headers' 'Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma';
# 处理预检请求
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;
}
# SpringBoot 应用访问路径
proxy_pass http://admin;
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-Forwarded-Proto $scheme;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
upstream admin {
server 172.17.0.6:8098;
}
server {
listen 80; # 监听的端⼝
server_name xxx.com; # 域名或ip
location / { # 访问路径配置
# root index;# 根⽬录
proxy_pass http://admin;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80; # 监听的端⼝
server_name xxx.com; # 域名或ip
location / { # 访问路径配置
# root index;# 根⽬录
proxy_pass http://admin;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
注:172.17.0.6 这个端口是容器的ip端口