XIKEW.COM - 公读宝典 - Nginx问题记录 - 公读宝典, -

Nginx问题记录
NGINX 4/14/2020 8:28:43 PM 阅读:11

反向代理配置

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $proxy_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache

    add_header Cache-Control no-cache;
    expires 12h;
}

跨域配置

#   预检命令的缓存,如果不缓存每次会发送两次请求
add_header Access-Control-Max-Age 3600;
#   带cookie请求需要加上这个字段,并设置为true
add_header Access-Control-Allow-Credentials true;

#   表示允许这个域跨域调用(客户端发送请求的域名和端口) 
#   $http_origin动态获取请求客户端请求的域   不用*的原因是带cookie的请求不支持*号
add_header Access-Control-Allow-Origin $http_origin;

#   表示请求头的字段 动态获取
add_header Access-Control-Allow-Headers 
$http_access_control_request_headers;

#   OPTIONS预检命令,预检命令通过时才发送请求
#   检查请求的类型是不是预检命令
if ($request_method = OPTIONS){
    return 200;
}

VUE history 模式

后端配置例子

location ^~ / {
    try_files $uri $uri/ /index.html;
}

搜索引擎蜘蛛反向代理

location ^~ / {
    set $flag "";
    if ($http_user_agent ~* (Spider|spider|Bot|bot)) {
      set $flag "http://127.0.0.1:8081/Xikew/Spider/Index";
    }
    if ($flag != "") {
      proxy_pass "${flag}";
    }
    proxy_set_header RequestUri $request_uri;
    proxy_set_header Host $proxy_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
}

no-www跳转到www

server {
    listen *:80;
    listen [::]:80;
    server_name example.com;
    return 301 http://www.example.com$request_uri;
}

错误提示

  • nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored

某个站点同时配置了域名和ip:port的两种解析