How to deploy multiple ports with nginx?
To deploy multiple ports on nginx, you can achieve this by configuring multiple server blocks. Below is a simple example configuration.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:3000; # 将请求转发到端口3000
}
}
server {
listen 8080;
server_name example.com;
location / {
proxy_pass http://localhost:4000; # 将请求转发到端口4000
}
}
In the configuration above, we defined two server blocks, each listening on port 80 and port 8080. Each server block has a location block to define the forwarding rules for requests. By specifying the target port in the proxy_pass directive, requests can be forwarded to different ports.
To apply the above configuration, you need to add these server blocks to the nginx configuration file and reload the nginx configuration. You can check if the syntax of the nginx configuration file is correct by using the following command.
sudo nginx -t
If there are no errors in the configuration file, you can use the following command to reload nginx:
sudo systemctl reload nginx
This will allow for successful deployment of multiple ports on nginx.