Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nginx配置 #10

Open
liuyidi opened this issue Mar 1, 2019 · 0 comments
Open

Nginx配置 #10

liuyidi opened this issue Mar 1, 2019 · 0 comments
Labels

Comments

@liuyidi
Copy link
Owner

liuyidi commented Mar 1, 2019

Nginx 配置采坑总结

1. nginx配置前后端分离

# nginx.conf/http/

server {
  listen 80;
  # 静态资源访问路径
  location ~* \.(html|js|css|png|jpg|jpeg|htm|ico|gif|ttf|woff)$ {
    root /home/xxxx;
  }
  # 默认页
  location / {
    root   html;
    index  index.html;
    # 配合browserHistory模式时使用
    try_files $uri $uri/ /index.html;
  }
  # 代理请求API
  location /api {
      proxy_pass http://api.liuyidi.com;
      proxy_set_header   X-Forwarded-Proto $scheme;
      proxy_set_header   Host              $http_host;
      proxy_set_header   X-Real-IP         $remote_addr;
  }  

  # 代理全局除去静态资源以外的资源
  location ~ .*$ {
    proxy_pass  http://api.liuyidi.com;
    proxy_pass_header Server;
    proxy_set_header X-Real_IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_http_version 1.1;
    proxy_connect_timeout 4s;                
    proxy_read_timeout 600s;             
    proxy_send_timeout 12s;                  
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
  }
}

2. nginx location匹配规则

模式 含义
location = /uri = 表示精确匹配,只有完全匹配上才能生效
location ^~ /uri ^~ 开头对URL路径进行前缀匹配,并且在正则之前。
location ~ pattern 开头表示区分大小写的正则匹配
location ~* pattern 开头表示不区分大小写的正则匹配
location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
location / 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default

3. nginx如何支持websocket

前端配置

function createWebSocket(basePath,serviceName){
    var websocket;
    if ('WebSocket' in window) {
        websocket = new WebSocket("ws://"+basePath+"/"+serviceName);
    } else if ('MozWebSocket' in window) {
	websocket = new MozWebSocket("ws://"+basePath+"/"+serviceName);
    } else {
	websocket = new SockJS("http://"+basePath+"/sockjs/"+serviceName);
    }
    websocket.onopen = function (e) {
    }
    websocket.onmessage= function (e) {
    }
    websocket.onerror= function (e) {
    }
    websocket.onclose= function (e) {
    }
}

服务端java配置

webSocketHandlerRegistry
  .addHandler(goodsWebsocketHandler(),"/websocket.do")
  .addInterceptors(new HandshakInterceptor())
  .setAllowedOrigins("*");
// 不使用sockjs
webSocketHandlerRegistry.addHandler(goodsWebsocketHandler(),"/sockjs/websocket.do").addInterceptors(new HandshakInterceptor()).withSockJS();

nginx配置

# nginx.conf/http/
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
server {
  location / {
    proxy_pass http://www.liuyidi.com;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header Origin '';
  }
}

4. nginx反向代理websocket报403错误问题

  • nginx1.3版本以后才支持websocket。
  • websocket配置nginx代理时服务端和前端的origin必须一致,设置服务端setAllowedOrigins("*")
  • 设置nginx proxy_set_header Origin ''

5. nginx开启gzip加速

# gzip配置
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";

6. websocket支持https出现ERR_SSL_PROTOCOL_ERROR的原因

wss://example.com:8088/websocket

ws 基于http下的一种协议,默认的服务端口是80.
wss 基于https下的一种协议,默认的服务端口是433,如果使用wss则也需要使用433的端口号而不是80

@liuyidi liuyidi added the nginx label Jul 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant