Implementation of Nginx configuration of multi-port and multi-domain name access

Implementation of Nginx configuration of multi-port and multi-domain name access

To deploy multiple sites on a server, you need to open multiple ports to access different sites. The process is very simple. It took 2 hours to debug. Here is a record:

Main domain name multi-port access

Set up an A record in the DNS NameServer

Point www.xxx.com to the server ip

Open the required ports and modify the nginx configuration file

For example, we have two services open on port 80 and port 8080 respectively.

If there is iptable, open the port first:

iptables -A INPUT -ptcp --dport 80 -j ACCEPT
iptables -A INPUT -ptcp --dport 8080 -j ACCEPT

Modify the configuration file:

#path: /usr/local/nginx/conf/nginx.conf

server {
listen 80;
server_name www.xxx.com;
access_log /data/www/log/33.33.33.33_nginx.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
root /data/www/website/33.33.33.33:80;


location ~ [^/]\.php(/|$) {
  fastcgi_pass unix:/dev/shm/php-cgi.sock;
  fastcgi_index index.php;
  include fastcgi.conf;
  }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  expires 30d;
  access_log off;
  }
location ~ .*\.(js|css)?$ {
  expires 7d;
  access_log off;
  }
}
server {
listen 8080;
server_name A.xxx.com;
access_log /data/www/log/33.33.33.33:8080_nginx.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
root /data/www/website/33.33.33.33:8080;


location ~ [^/]\.php(/|$) {
  fastcgi_pass unix:/dev/shm/php-cgi.sock;
  fastcgi_index index.php;
  include fastcgi.conf;
  }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
  expires 30d;
  access_log off;
  }
location ~ .*\.(js|css)?$ {
  expires 7d;
  access_log off;
  }
}

The key is the two server sections. You can also split these two sections into two configuration files and put them in

/etc/nginx/conf.d/

Below the directory;

Subdomain multi-port access

This kind of access is silly, because your access to port 8080 needs to be in the format of http://xxx.com:8080;

And if there are two different cgis, for example, port 80 corresponds to a php web service, and port 8080 corresponds to a nodejs web service; and our nodejs comes with a web service that is already listening on port 8080, what should we do?

At this time, we need the reverse proxy function of Nginx and add an A record on the DNS Server to finally achieve

  • www.xxx.com accesses port 80
  • A.xxx.com accesses port 8080 through nginx forwarding

Add an A record

Point A.xxx.com to the server ip

The Nginx configuration template is as follows:

#path: /usr/local/nginx/conf/nginx.conf

server {
  listen 80;
  server_name www.xxx.com;
  access_log /data/www/log/33.33.33.33_nginx.log combined;
  index index.html index.htm index.php;
  include /usr/local/nginx/conf/rewrite/none.conf;
  root /data/www/website/33.33.33.33:80;


  location ~ [^/]\.php(/|$) {
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
    expires 30d;
    access_log off;
    }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
}

server {
  listen 80;
  listen [::]:80;

  server_name A.XXX.com;

  proxy_connect_timeout 300s;
  proxy_send_timeout 300s;
  proxy_read_timeout 300s;
  fastcgi_send_timeout 300s;
  fastcgi_read_timeout 300s;

  location / {
    proxy_pass http://127.0.0.1:3000;
    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;
    try_files $uri $uri/ =404;
  }
}

nginx reloads the configuration file

nginx -s reload

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Solve the problem of modifying configuration access of tp5 under nginx
  • How to configure nginx to limit the access frequency of the same IP
  • Steps to configure nginx ssl to implement https access (suitable for novices)
  • How to configure Nginx to distinguish between PC or mobile phone access to different domain names
  • Detailed explanation of Nginx access restriction configuration
  • Detailed explanation of nginx using ssl module configuration to support HTTPS access
  • How to use nginx to configure access to wgcloud

<<:  Detailed explanation of the use of React list bar and shopping cart components

>>:  Detailed explanation of MySQL database addition, deletion and modification operations

Recommend

Vue3 compilation process-source code analysis

Preface: Vue3 has been released for a long time. ...

Causes and solutions for MySQL too many connections error

Table of contents Brief summary At noon today, th...

Detailed explanation of semiotics in Html/CSS

Based on theories such as Saussure's philosop...

Detailed explanation of the correct use of the if function in MySQL

For what I am going to write today, the program r...

Nginx handles http request implementation process analysis

Nginx first decides which server{} block in the c...

Specific use of Mysql prepare preprocessing

Table of contents 1. Preprocessing 2. Pretreatmen...

Detailed steps for installing and debugging MySQL database on CentOS7 [Example]

This example requires downloading and installing ...

Echarts Bar horizontal bar chart example code

Table of contents Horizontal bar chart Dynamicall...

Details on using order by in MySQL

Table of contents 1. Introduction 2. Main text 2....

js to achieve star flash effects

This article example shares the specific code of ...