nginx general domain name resolution configuration tutorial

  • 2020-05-12 06:57:42
  • OfStack

The last project needed to host a high concurrent request, so NodeJs was selected in the back-end language, but sometimes nodejs could not read the host name of the request, so I thought of using nginx+nodejs for processing.

Nginx configuration

The following code


upstream io_nodes {
server 127.0.0.1:8081;
}
server {
listen 80;
listen [::]:80;
root /var/www/html;
server_name ~^(?<subdomain>.+).example.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-AppId $subdomain;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://io_nodes;
}
}

Just restart the nginx server. nginx will reverse proxy all the **.example.com ** format domain names to the **8081** port of the machine, which is **nodejs** listening port.

NodeJs reads the requested domain name as well as the subdomain name

Using express


var host = req.headers.host;
var appid = req.headers['x-appid'];

Related articles: