Nginx reverse proxy configuration of proxy_pass

First, prepare the environment

Preparing analytical applications using the express route
would have been prepared using Flask, but could not find a route wildcard implementation

1、server.js

const express = require("express");

const app = express();

app.get("*", (request, response) => {
    response.send(request.path)
})

const port = 8081;

app.listen(port, () => {
    console.log(`server at: http://127.0.0.1:${port}`);
})

Visit: http: //127.0.0.1: 8081 /

2, the local configuration the DNS
/ etc / the hosts

127.0.0.1       www.demo.com

3, configure Nginx
demo.com.conf

server {
  listen 80;

  server_name www.demo.com;

  location / {
      proxy_pass http://127.0.0.1:8081/;
  }
}

Visit: http: //www.demo.com/

Second, a simple example

location matching URLs

location /      # 普通 location
location ^~ /   # ^表示 “非”,~ 表示 “正则”,意思是:不要继续匹配正则 

Look at a simple example:
Configuration 1:

location /api {
    proxy_pass http://127.0.0.1:8081/;
}

# 访问 | 返回两个路径
http://www.demo.com/api   /
http://www.demo.com/api/  //

Configuration 2:

location /api/ {
    proxy_pass http://127.0.0.1:8081/;
}

# 访问 | 返回一个路径
http://www.demo.com/api   /   # 网址会自动加/
http://www.demo.com/api/  /

Therefore, in order to reduce the variables, configuration location address all end with/

Third, the test

Address access are:
http://www.demo.com/api/index.html

proxy_pass return
http://127.0.0.1:8081 /api/index.html
http://127.0.0.1:8081/ /index.html
http://127.0.0.1:8081/admin /adminindex.html
http://127.0.0.1:8081/admin/ /admin/index.html

So, still proxy_pass to /end relatively easy to remember

Reference
nginx reverse proxy of proxy_pass

Released 1449 original articles · won praise 400 · Views 1.42 million +

Guess you like

Origin blog.csdn.net/mouday/article/details/105010864