簡體   English   中英

nginx虛擬主機,http服務器和tcp端口轉發

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

我有一個Nginx服務器,在example.com虛擬主機的端口上提供靜態html頁面。

還有兩個服務器在localhost:50001localhost:50002

我想通過以下方式將所有請求轉發到example.com

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

我該如何實現?

我能夠實現第一個,並開始聆聽5000150002

這是配置

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;
        }
}

我認為您需要反向代理。 遵循本指南的規則, 非常簡單。

首先,從主配置中刪除這些行(它也適用於默認配置,您只需要分開端口即可為每個端口創建新的server {}塊)。

listen 50001;
listen [::]:50001;

listen 50002;
listen [::]:50002;

事情是再創建兩個服務器塊配置,所以我們有/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;
  }
}

與端口50002上的應用程序相同。最后一件事是重新啟動Nginx服務器。

# systemctl restart nginx.service

希望這對你有用 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM