[英]nginx Virtual host, http server and tcp port forward
我有一個Nginx服務器,在example.com
虛擬主機的端口上提供靜態html頁面。
還有兩個服務器在localhost:50001
和localhost:50002
,
我想通過以下方式將所有請求轉發到example.com
example.com ---> /var/www/servers/example.com/
example.com:50001 ---> localhost:50001
example.com:50002 ---> localhost:50002
我該如何實現?
我能夠實現第一個,並開始聆聽50001
和50002
這是配置
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.