Nginx相关问题

前端页面刷新404

  vue单页因微信分享和自动登录需要,对于URL中存在’#’的地址,处理起来比较坑。用history模式就不会存在这样的问题。但是换成history模式,就会有个新的问题,就是页面刷新后,页面就无法显示了(404)。对于这个问题,我们只需要在服务器配置如果URL匹配不到任何静态资源,就跳转到默认的index.html。
server {
        listen       8888;
        server_name  localhost;
        root        E:/vue/my_project/dist;
        location / {
            try_files $uri $uri/ @router;
            index  index.html index.htm;
        }
 
        location @router {
            rewrite ^.*$ /index.html last;
        }
      
  }

解析远程真实IP

server {
        listen       8888;
        server_name  localhost;
        root        E:/vue/my_project/dist;
        location / {
            index  index.html index.htm;
            proxy_set_header X-Real-IP $remote_addr;
        }
      
  }

Windows下nginx

#启动
start nginx
 
#停止
nginx -s stop
 
#重启
nginx -s reload

Linux下ningx

#首先利用配置文件启动Nginx:
nginx -c /usr/local/nginx/conf/nginx.conf
 
重启服务: 
service nginx restart
 
#快速停止或关闭Nginx:
nginx -s stop
 
#正常停止或关闭Nginx:
nginx -s quit
 
# 配置文件修改重装载命令:
nginx -s reload

完整配置案例

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;

    upstream fabric {
        server 192.168.1.118:8085;
    }

    server {
        listen       9527;
        server_name  192.168.1.118;
        client_max_body_size    1000M;
        root   D:/software/nginx-1.14.0/dist;

        location / {
            try_files $uri $uri/ @router;
            index  index.html index.htm;
            proxy_set_header X-Real-IP $remote_addr;
        }
        
        location @router {
            rewrite ^.*$ /index.html last;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

nginx转发携带请求头,nginx转发headers内容丢失解决办法

背景:我通过nginx转发请求,请求头中携带   app_id   字段,我们项目用这个作为客户端识别的信息,但是后端没有接收到这个字段,获取不到值。

原因:nginx不会识别"_"这个符号,默认情况下它会忽略,所以后端没接收到,也就是说请求时候没将这个 app_id 转发过去导致的。

 

解决办法又两种:

1. 修改nginx配置

在 nginx 的 http 部分添加如下:

underscores_in_headers on; (默认 underscores_in_headers 为off)

2. 修改这个字段,取消下划线

列如 把原来的 app_id  换为  app-id 

posted @ 2020-01-09 11:35  itwetouch  阅读(276)  评论(0编辑  收藏  举报