nginx配置参考
这里只是参考文件,根据自己的项目需求自行配置
# 后端服务地址
upstream api_service {
server xxx.xxx.xxx.xxx:xxxx;
keepalive 2000;
}
server {
listen 80;
server_name xxx.com;
root /usr/local/nginx/html;
location / {
index index.html;
try_files $uri $uri/ /index.html; #react-router 防止页面刷新出现404
}
# 静态文件缓存,启用Cache-Control: max-age、Expires
location ~ ^/(css|js|media)/ {
expires 10y;
access_log off;
add_header Cache-Control "public";
}
# 代理ajax请求
location ^~/api {
rewrite ^/api/(.*)$ /$1 break; # 如果后端接口不是统一以api开头,去掉api前缀
proxy_pass http://api_service/;
proxy_set_header Host $http_host;
proxy_set_header Connection close;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Server $host;
}
}