全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 1369|回复: 11

nginx 配置转发笔记

[复制链接]
发表于 2020-6-23 15:10:45 | 显示全部楼层 |阅读模式
nginx 不仅仅可以 web 服务器也可以作为反向代理,所以不如试试用 nginx 来进行中转?
讲讲优点,比如维持长连接(不必要频繁握手),异步 I/O 支持(天然的并发),正儿八经的 tls(所以那些所谓的隧道使用的 ws+tls 就可以使用 nginx 代替)
以中转 ws+tls 为例:
在这里先 bia 一个配置文件,
nginx 的配置文件(nginx.conf)
请根据实际情况调整


  1. user www-data;
  2. worker_processes 2;
  3. pid /run/nginx.pid;
  4. include /etc/nginx/modules-enabled/*.conf;

  5. events {
  6.   worker_connections 1024;
  7.   # multi_accept on;
  8. }

  9. http {

  10.   ##
  11.   # Basic Settings
  12.   ##

  13.   sendfile on;
  14.   tcp_nopush on;
  15.   tcp_nodelay on;
  16.   keepalive_timeout 120; # 设置长连接超时时间
  17.   keepalive_requests 16368; # 每个长连接最大允许多少个请求
  18.   types_hash_max_size 2048;

  19.   # server_names_hash_bucket_size 64;
  20.   # server_name_in_redirect off;

  21.   include /etc/nginx/mime.types;
  22.   default_type application/octet-stream;

  23.   ##
  24.   # SSL Settings
  25.   ##

  26.   ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
  27.   ssl_prefer_server_ciphers on;

  28.   ##
  29.   # Logging Settings
  30.   ##

  31.   access_log /var/log/nginx/access.log;
  32.   error_log /var/log/nginx/error.log;

  33.   ##
  34.   # Gzip Settings
  35.   ##

  36.   gzip on;

  37.   # gzip_vary on;
  38.   # gzip_proxied any;
  39.   # gzip_comp_level 6;
  40.   # gzip_buffers 16 8k;
  41.   # gzip_http_version 1.1;
  42.   # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

  43.   ##
  44.   # Virtual Host Configs
  45.   ##
  46.   map $http_upgrade $connection_upgrade {
  47.     default upgrade;
  48.     '' close;
  49.   }
  50.   include /etc/nginx/conf.d/*.conf;
  51.   include /etc/nginx/sites-enabled/*;
  52. }

复制代码


站点配置文件
请根据实际情况调整
  1. server {
  2.   listen 443 ssl http2;
  3.   listen [::]:443 ssl http2;
  4.   server_name localhost.localdomain;
  5.   ssl_certificate /etc/ssl/cert.pem;
  6.   ssl_certificate_key /etc/ssl/key.pem;
  7.   ssl_prefer_server_ciphers on;


  8.   location / {
  9.     resolver 114.114.114.114 8.8.8.8;
  10.     proxy_pass https://remotehost.remotedomain;
  11.     proxy_read_timeout 90s;
  12.     proxy_send_timeout 90s;
  13.     proxy_redirect https://remotehost.remotedomain /;
  14.     proxy_ssl_server_name on;
  15.     proxy_connect_timeout 500s;
  16.     proxy_http_version 1.1;
  17.     proxy_set_header X-Real-IP $remote_addr;
  18.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  19.     proxy_set_header Upgrade $http_upgrade;
  20.     proxy_set_header Connection $connection_upgrade;
  21.   }
  22. }
复制代码


以上配置,所谓的 ws+tls 隧道配置结束,
nginx 在 1.17 版本支持 aio thread,如果有兴趣可以继续讨论,如果你会编写 lua 使用 openresty 会更加舒服
发表于 2020-6-23 15:31:42 | 显示全部楼层
本帖最后由 88232128 于 2020-6-23 15:35 编辑
  1. http{
  2.     # websocket
  3.     map $http_upgrade $connection_upgrade {
  4.         default upgrade;
  5.         '' close;
  6.     }
  7. }

  8. # websocket
  9. upstream websocket {
  10.     server xxx.xxx.xxx.xxx:8082;
  11.     server xxx.xxx.xxx.xxx:8082;
  12. }

  13. server {
  14.     # websocket
  15.     location ^~ /websocket/ {
  16.     proxy_pass http://websocket;
  17.     proxy_set_header Host $host;
  18.     proxy_set_header X-Real-IP $remote_addr;
  19.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  20.     proxy_http_version 1.1;
  21.     proxy_set_header Upgrade $http_upgrade;
  22.     proxy_set_header Connection $connection_upgrade;
  23.     }
  24. }
复制代码
发表于 2020-6-23 15:13:36 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| 发表于 2020-6-23 15:16:50 | 显示全部楼层
朕的大清完了? 发表于 2020-6-23 15:13
resolver 114.114.114.114 8.8.8.8; 这是干嘛的

指定一个 DNS 解析,不然 nginx 不清楚 remotehost.remotedomain 的 IP
发表于 2020-6-23 15:21:46 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2020-6-23 15:26:52 | 显示全部楼层
恭喜我升级了
发表于 2020-6-23 15:27:51 | 显示全部楼层
升级一下,加个缓存
发表于 2020-6-23 15:36:30 | 显示全部楼层
你这个不错
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-27 23:28 , Processed in 0.093890 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表