Nginx profiles and virtual hosts

Keywords: PHP Nginx

catalogue

1, Configuration file

1. Access statistics

2. Customer based access control

2, Virtual host

1. Domain name based virtual web host

Configuration steps:

2. Port based virtual web host

Configuration steps:

3. ip based virtual web host

Configuration steps:

summary

1, Configuration file

1. Access statistics

You can also map the website in the / etc/hosts file in advance. You need to access it in the browser of the virtual machine during the test

1,Use the command first/usr/local/nginx/sbin/nginx -V View installed Nginx Include HTTP_STUB_STATUS modular
2,modify nginx.conf Configuration file, specify access location and add stub_status to configure

cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.ly.com;
		charset utf-8;
		location / {
			root html;
			index index.html index.php;
		}
		##add to stub_status to configure##
		location /status { 					#The access location is / status
			stub_status on; 				#Turn on the status statistics function
			access_log off; 				#Turn off logging at this location
		}
	}
}

systemctl restart nginx

2. Customer based access control

The access control rules are as follows:
deny IP/IP Segment: reject a IP or IP Client access to the segment.
allow IP/IP Segment: allow a IP or IP Client access to the segment.
The rule is executed from top to bottom. If it matches, it will stop and no longer match from bottom to top.

vim /usr/local/nginx/conf/nginx.conf
......
	server {
		location / {
			......
			##Add control rule##
			deny 192.168.255.230; 					#Access denied client IP
			allow all;								#Allow other IP clients to access
		}
	}

systemctl restart nginx

2, Virtual host

Improve the utilization rate of host, save cost and save host

1. Domain name based virtual web host

Purpose: visit different pages by visiting different domain names

Configuration steps:

Add domain name resolution

echo "192.168.255.180 www.ly.com www.ly2.com" >> /etc/hosts

Preparing web documents for virtual sites

mkdir -p /var/www/html/ly
mkdir -p /var/www/html/ly2
echo "<h1>www.ly.com</h1>" > /var/www/html/ly/index.html
echo "<h1>www.ly2.com</h1>" > /var/www/html/ly2/index.html

Modify profile

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.ly.com;					#Set domain name www.ly.com
		charset utf-8;
		access_log logs/www.ly.access.log; 
		location / {
			root /var/www/html/ly;				#Set the working directory of www.ly.com
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
	
	server {
		listen 80;
		server_name www.ly2.com;					#Set domain name www.ly2.com
		charset utf-8;
		access_log logs/www.ly2.access.log; 
		location / {
			root /var/www/html/ly2;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}	
}

  Restart test

systemctl restart nginx

stay centos Test in the system
http://www.ly.com
http://www.ly2.com

2. Port based virtual web host

Purpose: access different pages through different port numbers

Configuration steps:

Add domain name resolution

echo "192.168.255.180 www.ly.com www.ly8080.com" >> /etc/hosts

Preparing web documents for virtual sites

mkdir -p /var/www/html/ly
mkdir -p /var/www/html/ly8080
echo "<h1>www.ly.com</h1>" > /var/www/html/ly/index.html
echo "<h1>www.ly8080.com</h1>" > /var/www/html/ly8080/index.html

Modify profile

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 192.168.255.180:80;					#Set listening port 80
		server_name www.ly.com;
		charset utf-8;
		access_log logs/www.ly.access.log; 
		location / {
			root /var/www/html/ly;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}

server {
	listen 192.168.255.180:8080;					#Set listening 8080 port
	server_name www.ly.com;
	charset utf-8;
	access_log logs/www.ly8080.access.log; 
	location / {
		root /var/www/html/ly8080;
		index index.html index.php;
	}
	error_page 500 502 503 504 /50x.html;
	location = 50x.html{
		root html;
	}
}

systemctl restart nginx

Restart test

systemctl restart nginx

stay centos Test in the system
192.168.255.180:80
192.168.255.180:8080

3. ip based virtual web host

Objective: to access different pages through different network cards

Configuration steps:

Add domain name resolution

echo "192.168.255.180 www.ly.com " >> /etc/hosts
echo "192.168.255.100 www.ly2100.com " >> /etc/hosts

Preparing web documents for virtual sites

mkdir -p /var/www/html/ly
mkdir -p /var/www/html/ly2100
echo "<h1>www.ly.com</h1>" > /var/www/html/ly/index.html
echo "<h1>www.ly2100.com</h1>" > /var/www/html/ly2100/index.html

Create temporary virtual network card

ifconfig ens33:0 192.168.255.100 netmask 255.255.255.0 

Modify profile

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
		listen 192.168.255.180:80;					#Set listening address
		server_name www.ly.com;
		charset utf-8;
		access_log logs/www.ly.access.log; 
		location / {
			root /var/www/html/ly;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
server {
	listen 192.168.255.100:80;					#Set listening address
	server_name www.ly2100.com;
	charset utf-8;
	access_log logs/www.ly2100.access.log; 
	location / {
		root /var/www/html/ly2100;
		index index.html index.php;
	}
	error_page 500 502 503 504 /50x.html;
	location = 50x.html{
		root html;
	}
}	
}

Restart test

systemctl restart nginx

stay centos Test in the system
192.168.255.180
192.168.255.100

summary

  When customers access Ng through http service, they first shake hands three times. Through different requests, we usually call through different interfaces of the same domain name. We can jump to different business areas and provide different requirements. When calling database resources, we also make full use of virtual host to realize it

Posted by lakshmiyb on Fri, 08 Oct 2021 02:11:56 -0700