nginx反代ngrok的问题

我的机器上部署了nginx与ngrok但是我需要让两个程序都使用80和443
我现在ngrok监听81,444然后nginx反代

反代配置如下

server {    
    listen  80;  
    server_name *.0n0.win;  
      
    root   html;
    index  index.html index.htm index.php;
 
    ## send request back to apache ##
    location / {
        proxy_pass  http://127.0.0.1:81;
        #Proxy Settings
        proxy_redirect     off;
        #proxy_set_header Host downloads.openwrt.org;
        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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}  

server {
    listen 443;
    server_name *.0n0.win;
 
    ssl on;
    ssl_certificate /etc/letsencrypt/live/shirakun.cc/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/shirakun.cc/privkey.pem;
#    access_log  logs/quancha.access.log  main;
#    error_log  logs/quancha.error.log;
    root   html;
    index  index.html index.htm index.php;
 
    ## send request back to apache ##
    location / {
        proxy_pass  https://127.0.0.1:444;
        #Proxy Settings
        proxy_redirect     off;
        #proxy_set_header Host downloads.openwrt.org;
        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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}

但是,现在出现一个问题,我映射后直接访问仍提示隧道未映射,然后还是得在域名后面加上:81才能访问,请问如何解决

阅读 9.8k
3 个回答
server {    
    listen  80;  
    server_name *.0n0.win;  
      
    root   html;
    index  index.html index.htm index.php;
 
    ## send request back to apache ##
    location / {
        proxy_pass  http://127.0.0.1:81;
        #Proxy Settings
        proxy_redirect     off;
        #proxy_set_header Host downloads.openwrt.org;
        proxy_set_header   Host             $host:81;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}  

server {
    listen 443;
    server_name *.0n0.win;
 
    ssl on;
    ssl_certificate /etc/letsencrypt/live/shirakun.cc/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/shirakun.cc/privkey.pem;
#    access_log  logs/quancha.access.log  main;
#    error_log  logs/quancha.error.log;
    root   html;
    index  index.html index.htm index.php;
 
    ## send request back to apache ##
    location / {
        proxy_pass  https://127.0.0.1:444;
        #Proxy Settings
        proxy_redirect     off;
        #proxy_set_header Host downloads.openwrt.org;
        proxy_set_header   Host             $host:444;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}
新手上路,请多包涵

我也被这个问题坑到了。

解决方法就是注释掉

proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

这行。

如果nginx反代的同时又转发了真实的IP地址,ngrok会收到两个ip导致冲突。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏