[英]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.