I have an nginx server which serves a static html page on port for example.com
virtual host.
There are two more servers running on localhost:50001
and localhost:50002
,
I want to forward all requests to example.com
in following way
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
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).
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
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.
# systemctl restart nginx.service
Hope it works for you :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.