简体   繁体   English

如何使用NGINX反向代理,并代理上游URL的端口和路径?

[英]How to use NGINX reverse proxy, and proxy the port and path to the upstream url?

I've set up nginx reverse proxy as per the instructions here: https://medium.com/@mightywomble/how-to-set-up-nginx-reverse-proxy-with-lets-encrypt-8ef3fd6b79e5 我按照以下说明设置了nginx反向代理: https//medium.com/@mightywomble/how-to-set-up-nginx-reverse-proxy-with-lets-encrypt-8ef3fd6b79e5

My sites-enabled/mysite.conf looks like 我的sites-enabled / mysite.conf看起来像

server {      
 server_name mynginxreverseproxy.com; 
 set $upstream serverip:$server_port/; 
 location / {  
    proxy_pass_header Authorization;  
    proxy_pass http://$upstream;  
    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 Connection “”;  
    proxy_buffering off;  
    client_max_body_size 0;  
    proxy_read_timeout 36000s;  
    proxy_redirect off;  }

    listen 443 ssl; # paths to ssl cert files

} server {
    if ($host = mynginxreverseproxy.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


 server_name mynginxreverseproxy.com; listen 80;
    return 404; 

}

I would like to forward the port and path to my server, such as https://mynginxreverseproxy.com:portnumber/myurl -> to serverip:com:portnumber/myurl. 我想将端口和路径转发到我的服务器,例如https://mynginxreverseproxy.com:portnumber/myurl - >到serverip:com:portnumber / myurl。 I've been able to get this working for a single port which can forward port 80 to my desired port, but how to I scale it out to work for many ports? 我已经能够将这个工作用于单个端口,它可以将端口80转发到我想要的端口,但是如何将其扩展为适用于许多端口?

I have many port mapping that I'd like to set up, so it would be ideal to do this using a variable, rather than hardcoding each port mapping. 我有许多我想要设置的端口映射,因此使用变量进行此操作是理想的,而不是硬编码每个端口映射。

How do I pass the port and path through using a variable in the conf file? 如何通过使用conf文件中的变量传递端口和路径?

I eventually got this working after a lot of debugging and configurations. 经过大量的调试和配置后,我最终得到了这个功能。

The

set $upstream serverip:$server_port

was correct, but I was missing the 是对的,但我错过了

listen directive which allows the NGINX server to listen on a particular port, it listens on port 80 by default. listen指令允许NGINX服务器监听特定端口,它默认监听端口80。 However, port 80 was not the port which I wanted to get passed through to the upstream server. 但是,端口80不是我想要传递到上游服务器的端口。

Perhaps someone would find it useful, there is an online utility where you can generate an nginx configuration. 也许有人会发现它很有用,有一个在线实用程序,您可以在其中生成nginx配置。 Very user friendly 非常用户友好

You can find it here 你可以在这里找到它

Another way to do this (sample config below) 另一种方法(下面的示例配置)

upstream myserver {
    server ip-address:port;
}

server {
  listen 80;      //IPv4
  listen [::]:80; //IPv6
  location / {
    proxy_pass http://myserver /;
    proxy_set_header Host $http_host;
    ...rest of your headers...
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM