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

Typora code block color matching and title serial number implementation code

Effect: The title has its own serial number, the ...

Summarize the common properties of BigIn functions in JavaScript

Table of contents 1. Overview 2. Attributes 1. Ma...

Example of how to set automatic creation time and modification time in mysql

This article describes how to set the automatic c...

Detailed explanation of using top command to analyze Linux system performance

Introduction to Linux top command The top command...

Add unlimited fonts to your website with Google Web Fonts

For a long time, website development was hampered...

Example analysis of MySQL startup and connection methods

Table of contents How to start mysqld Method 1: m...

Core skills that web front-end development engineers need to master

The content involved in Web front-end development...

Windows 10 installation vmware14 tutorial diagram

Software Download Download software link: https:/...

Defining the minimum height of the inline element span

The span tag is often used when making HTML web p...

MySQL account password modification method (summary)

Preface: In the daily use of the database, it is ...

Two ways to achieve horizontal arrangement of ul and li using CSS

Because li is a block-level element and occupies ...

User needs lead to marketing-oriented design

<br />For each of our topics, the team will ...

Vue routing lazy loading details

Table of contents 1. What is lazy loading of rout...

What is the function of !-- -- in HTML page style?

Mainly for low version browsers <!-- --> is ...