简体   繁体   English

nginx虚拟主机,http服务器和tcp端口转发

[英]nginx Virtual host, http server and tcp port forward

I have an nginx server which serves a static html page on port for example.com virtual host. 我有一个Nginx服务器,在example.com虚拟主机的端口上提供静态html页面。

There are two more servers running on localhost:50001 and localhost:50002 , 还有两个服务器在localhost:50001localhost:50002

I want to forward all requests to example.com in following way 我想通过以下方式将所有请求转发到example.com

example.com          --->      /var/www/servers/example.com/
example.com:50001    --->      localhost:50001
example.com:50002    --->      localhost:50002

How can I achieve this? 我该如何实现?

I am able to achieve the first one, and started listening on 50001 and 50002 我能够实现第一个,并开始聆听5000150002

Here is the config 这是配置

server {
        listen 80;
        listen [::]:80;

        listen 50001;
        listen [::]:50001;

        listen 50002;
        listen [::]:50002;

        root /var/www/servers/example.com/;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name example.com;

        location / {
                try_files $uri $uri/ =404;
        }


         location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

        }

        location ~ /\.ht {
                deny all;
        }
}

I think a reverse proxy is what you need. 我认为您需要反向代理。 Following the rules of this guide , it's pretty simple. 遵循本指南的规则, 非常简单。

First, delete those lines from the main config, (it also works on the default config, you just have to separate ports creating new server {} blocks to each one). 首先,从主配置中删除这些行(它也适用于默认配置,您只需要分开端口即可为每个端口创建新的server {}块)。

listen 50001;
listen [::]:50001;

listen 50002;
listen [::]:50002;

The thing is create two more Server Blocks config, so we have the /etc/nginx/sites-available/exampleat50001.com 事情是再创建两个服务器块配置,所以我们有/etc/nginx/sites-available/exampleat50001.com

server {
  listen 50001;

  server_name example.com;

  location / {
    proxy_pass http://localhost:50001;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

The same with the app at port 50002. The last thing is restart the nginx server. 与端口50002上的应用程序相同。最后一件事是重新启动Nginx服务器。

# systemctl restart nginx.service

Hope it works for you :) 希望这对你有用 :)

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

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