Nginx port mapping configuration method

  • 2020-05-14 06:06:34
  • OfStack

Before because of

The traditional way to set up a website is to use an web server to parse the file entry file, such as Nginx and Apache to parse the corresponding entry file. However, with the development of technology, some languages can start their own web service, such as Node and PHP. By default, starting an web service with the development language can only be accessed locally, such as http://localhost:8000/ http://127.0.0.1:8000/

However, in the development and debugging environment of Centos, the testing is done with window's browser, so we need to use an intermediate web server for port mapping

Nginx port mapping configuration


server {
  listen    80;
  server_name rbac.dev-lu.com;

  # 80 Forwarded to the 8000 port 
  location / {
      proxy_pass http://127.0.0.1:8000;
  }
}

nginx reverse proxy - multi-port mapping

Code interpretation

1.1 http: www baidu. test. com default is 80, access to "/" using a reverse proxy, then access to the local 8083;
1.2 8083 represents the local front-end project access address, the front-end needs to access the background data, "/", continue to proxy to the background address 9803;
1.3 in this way, multiple port access can be completed as long as port 80 is opened.
1.4 the root configuration can be either an absolute or a relative path.


 server {
    listen    80;
    server_name www.baidu.test.com;# The domain name you want to fill in is separated by commas 
    location / {
      proxy_pass http://localhost:8083; 
      proxy_set_header Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      root  /app/esop_web/esopschool;
      index index.html;
      try_files $uri $uri/ /index.html;
    }
    location /rest{
      proxy_pass http://localhost:9803; 
      proxy_set_header  Host  $host; 
      proxy_set_header  X-Real-IP  $remote_addr; 
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
    }
  }

Related articles: