设为首页收藏本站

简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย Français

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 76|回复: 8

nginx 配置转发笔记

[复制链接]

19

主题

136

回帖

391

积分

中级会员

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


[ol]
  • user www-data;
  • worker_processes 2;
  • pid /run/nginx.pid;
  • include /etc/nginx/modules-enabled/*.conf;
  • events {
  •   worker_connections 1024;
  •   # multi_accept on;
  • }
  • http {
  •   ##
  •   # Basic Settings
  •   ##
  •   sendfile on;
  •   tcp_nopush on;
  •   tcp_nodelay on;
  •   keepalive_timeout 120; # 设置长连接超时时间
  •   keepalive_requests 16368; # 每个长连接最大允许多少个请求
  •   types_hash_max_size 2048;
  •   # server_names_hash_bucket_size 64;
  •   # server_name_in_redirect off;
  •   include /etc/nginx/mime.types;
  •   default_type application/octet-stream;
  •   ##
  •   # SSL Settings
  •   ##
  •   ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
  •   ssl_prefer_server_ciphers on;
  •   ##
  •   # Logging Settings
  •   ##
  •   access_log /var/log/nginx/access.log;
  •   error_log /var/log/nginx/error.log;
  •   ##
  •   # Gzip Settings
  •   ##
  •   gzip on;
  •   # gzip_vary on;
  •   # gzip_proxied any;
  •   # gzip_comp_level 6;
  •   # gzip_buffers 16 8k;
  •   # gzip_http_version 1.1;
  •   # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  •   ##
  •   # Virtual Host Configs
  •   ##
  •   map $http_upgrade $connection_upgrade {
  •     default upgrade;
  •     '' close;
  •   }
  •   include /etc/nginx/conf.d/*.conf;
  •   include /etc/nginx/sites-enabled/*;
  • }
  • [/ol]复制代码

    站点配置文件
    请根据实际情况调整
    [ol]
  • server {
  •   listen 443 ssl http2;
  •   listen [::]:443 ssl http2;
  •   server_name localhost.localdomain;
  •   ssl_certificate /etc/ssl/cert.pem;
  •   ssl_certificate_key /etc/ssl/key.pem;
  •   ssl_prefer_server_ciphers on;
  •   location / {
  •     resolver 114.114.114.114 8.8.8.8;
  •     proxy_pass https://remotehost.remotedomain;
  •     proxy_read_timeout 90s;
  •     proxy_send_timeout 90s;
  •     proxy_redirect https://remotehost.remotedomain /;
  •     proxy_ssl_server_name on;
  •     proxy_connect_timeout 500s;
  •     proxy_http_version 1.1;
  •     proxy_set_header X-Real-IP $remote_addr;
  •     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  •     proxy_set_header Upgrade $http_upgrade;
  •     proxy_set_header Connection $connection_upgrade;
  •   }
  • }[/ol]复制代码

    以上配置,所谓的 ws+tls 隧道配置结束,
    nginx 在 1.17 版本支持 aio thread,如果有兴趣可以继续讨论,如果你会编写 lua 使用 openresty 会更加舒服
  • 回复

    使用道具 举报

    121

    主题

    2569

    回帖

    5673

    积分

    论坛元老

    积分
    5673
    发表于 2020-6-23 15:31:42 | 显示全部楼层
    本帖最后由 88232128 于 2020-6-23 15:35 编辑
    [ol]
  • http{
  •     # websocket
  •     map $http_upgrade $connection_upgrade {
  •         default upgrade;
  •         '' close;
  •     }
  • }
  • # websocket
  • upstream websocket {
  •     server xxx.xxx.xxx.xxx:8082;
  •     server xxx.xxx.xxx.xxx:8082;
  • }
  • server {
  •     # websocket
  •     location ^~ /websocket/ {
  •     proxy_pass http://websocket;
  •     proxy_set_header Host $host;
  •     proxy_set_header X-Real-IP $remote_addr;
  •     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  •     proxy_http_version 1.1;
  •     proxy_set_header Upgrade $http_upgrade;
  •     proxy_set_header Connection $connection_upgrade;
  •     }
  • }[/ol]复制代码
  • 回复

    使用道具 举报

    43

    主题

    778

    回帖

    1797

    积分

    金牌会员

    积分
    1797
    发表于 2020-6-23 15:11:33 | 显示全部楼层
    标记一下
    回复

    使用道具 举报

    10

    主题

    277

    回帖

    616

    积分

    高级会员

    积分
    616
    发表于 2020-6-23 15:13:36 | 显示全部楼层
    mark zsbd
    回复

    使用道具 举报

    143

    主题

    728

    回帖

    1957

    积分

    金牌会员

    积分
    1957
    发表于 2020-6-23 15:13:36 | 显示全部楼层

    朕的大清完了? 发表于 2020-6-23 15:13

    resolver 114.114.114.114 8.8.8.8; 这是干嘛的

    指定一个 DNS 解析,不然 nginx 不清楚 remotehost.remotedomain 的 IP
    回复

    使用道具 举报

    19

    主题

    136

    回帖

    391

    积分

    中级会员

    积分
    391
     楼主| 发表于 2020-6-23 15:16:50 | 显示全部楼层
    恭喜我升级了
    回复

    使用道具 举报

    143

    主题

    728

    回帖

    1957

    积分

    金牌会员

    积分
    1957
    发表于 2020-6-23 15:13:00 | 显示全部楼层
    升级一下,加个缓存
    回复

    使用道具 举报

    65

    主题

    909

    回帖

    2057

    积分

    金牌会员

    积分
    2057
    发表于 2020-6-23 15:21:46 | 显示全部楼层
    你这个不错
    朕的大清完了? 发表于 2020-6-23 15:21

    意思是反代 都建议加上吗。

    会不会 。。。

    你也可以指定 IP,但是 remote 应该不会暴露 https://ip 吧?
    回复

    使用道具 举报

    40

    主题

    767

    回帖

    1812

    积分

    金牌会员

    积分
    1812
    发表于 2020-6-23 15:26:52 | 显示全部楼层

    88232128 发表于 2020-6-23 15:31

    写出来 upstream 更好,这咱不是懒嘛
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|Discuz! X

    GMT+8, 2026-3-28 23:33 , Processed in 0.020408 second(s), 4 queries , Gzip On, Redis On.

    Powered by Discuz! X3.5

    © 2001-2025 Discuz! Team.

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