Nginx 配置示例

puma应用

upstream myapp {
  server tcp://0.0.0.0:3001;
}

server {
  listen 80;
  server_name www.one.work;
  
  location /cable {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
  
  location / {
    root /opt/srv/xxxx/current/public;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_pass http://myapp;
  }
}

请求转发

server {
  listen 80;
  server_name wechat.one.work;
  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_redirect off;
    proxy_pass http://xx.xx.xxx.xx:20000;  # 外网IP
  }
}

websocket

  location /cable {
    proxy_pass  http://localhost:3001;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

日志分割

/opt/nginx/logs/*.log {  
  daily         # 按日阶段   
  minsize 1M    # 文件容量超過1m才進行,忽略時間參數     
  missingok     # 如果日志不存在则忽略该警告信息 
  rotate 7      # 保留7天 
  compress      # 压缩  
  delaycompress # 不压缩前一个截断的文件(需要与compress一起用)
  notifempty    # 增加日期作为后缀,不然会是一串无意义的数字  
  copytruncate  # 清空原有文件,而不是创建一个新文件
}