Build nginx virtual host based on domain name, port and IP

Build nginx virtual host based on domain name, port and IP

There are three types of virtual hosts supported by nginx

1. Domain name-based virtual hosting

2. IP-based virtual hosting

3. Port-based virtual hosts

1. Building based on domain name

1. Compile and install nginx service

2. Configure DNS domain name resolution service

3. Configure virtual host

a. Create a self-test web page

[root@localhost named]# cd 
[root@localhost ~]# mkdir -p /var/www/html/kgc
[root@localhost ~]# mkdir -p /var/www/html/accp
[root@localhost ~]# ls /var/www/html/accp kgc
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this kgc web" > kgc/index.html
[root@localhost html]# echo "this accp web" > accp/index.html

b. Edit the nginx.conf configuration file

vim /usr/local/nginx/conf/nginx.conf
 include conf.d/*.conf;
 server {
  listen 80;
  server_name www.kgc.com;
  charset utf-8;
  access_log logs/www.kgc.com.access.log;
  location / {
   root /var/www/html/kgc;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }
 server {
  listen 80;
  server_name www.accp.com;
  charset utf-8;
  access_log logs/www.accp.com.access.log;
  location / {
   root /var/www/html/accp;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }

c. Overload service

systemctl restart nginx
netstat -ntap | grep 80

d. Access test

www.kgc.com
www.accp.com

2. Port-based

a. Create a test webpage for another port

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is kgc 8080 web" > kgc/index.html

b. Edit the nginx.conf configuration file and only modify the listening address

server {
  listen 192.168.109.137:80;
  server_name www.accp.com;
  charset utf-8;
  access_log logs/www.accp.com.access.log;
  location / {
   root /var/www/html/accp;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }
 server {
  listen 192.168.109.137:8080;
  server_name www.accp.com;
  charset utf-8;
  access_log logs/www.accp8080.com.access.log;
  location / {
   root /var/www/html/accp8080;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }

c. Reload nginx service

systemctl restart nginx
netstat -ntap | grep 80

d. Test web page

www.accp.com
www.accp.com8080

3. Based on IP

1. Modify the regional data configuration file in the web page configuration file

vim /var/named/kgc.com.zone
systemctl restart named

2. Edit the configuration in nginx.conf and modify the IP address

server {
  listen 192.168.109.137:80;
  server_name www.kgc.com;
  charset utf-8;
  access_log logs/www.kgc.com.access.log;
  location / {
   root /var/www/html/kgc;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }
 server {
  listen 192.168.109.134:80;
  server_name www.accp.com;
  charset utf-8;
  access_log logs/www.accp.com.access.log;
  location / {
   root /var/www/html/accp;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }

c. Reload nginx service

systemctl restart nginx
netstat -ntap | grep 80

d. Test web page

192.168.109.137
192.168.109.134

Summarize

The above is what I introduced to you about building nginx virtual hosts based on domain names, ports, and IP addresses. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Implementation steps for building Nginx virtual host

<<:  Import backup between mysql database and oracle database

>>:  List rendering instructions for efficient development of Vue front-end

Recommend

Detailed explanation of 7 SSH command usages in Linux that you don’t know

A system administrator may manage multiple server...

Several common ways to deploy Tomcat projects [tested]

1 / Copy the web project files directly to the we...

Method of building redis cluster based on docker

Download the redis image docker pull yyyyttttwwww...

React+ts realizes secondary linkage effect

This article shares the specific code of React+ts...

Summary of MySQL development standards and usage skills

1. Naming conventions 1. Database names, table na...

Specific use of useRef in React

I believe that people who have experience with Re...

How to make Python scripts run directly under Ubuntu

Let’s take the translation program as an example....

Steps for IDEA to integrate Docker to achieve remote deployment

1. Enable remote access to the docker server Log ...

Pure CSS and Flutter realize breathing light effect respectively (example code)

Last time, a very studious fan asked if it was po...

Sample code for testing technology application based on Docker+Selenium Grid

Introduction to Selenium Grid Although some new f...

Detailed explanation of how to use the Vue license plate search component

A simple license plate input component (vue) for ...

Summary of Linux vi command knowledge points and usage

Detailed explanation of Linux vi command The vi e...

The perfect solution for Vue routing fallback (vue-route-manager)

Table of contents Routing Manager background gett...