编程知识 cdmana.com

Nginx configuring virtual hosts

nginx Virtual host

Good configuration nginx after Browser input IP Address can be opened nginx The welcome page

If you can't open it, it may be a firewall problem , You can turn off the firewall , Or add rules

firewalld-cmd --add-port=80/tcp  --permanent    Add rules 80 port --permanent Permanent 
firewalld-cmd --reload     Refresh to take effect           

     
  • 1.
  • 2.
Add virtual host

edit nginx Configuration file for

vim /etc/nginx/conf.d/default.conf      ( My profile is in etc Next )

     
  • 1.

nginx Configure the virtual host _nginx

 Change add   The virtual host address that needs to be set   ”www.aaa.com“

     
  • 1.
nginx -t    Check whether there are errors after changing the configuration 
nginx -s reload     Overload configuration   ( Refresh )

     
  • 1.
  • 2.

At this time, the virtual host address added to the web page cannot be accessed , You need to add the assignment to hosts

Linux Let's designate hosts

vim /etc/hosts     edit hosts file   Add the host address , This is the time Linux Next ping  The domain name and address of the host can be accessed 

     
  • 1.

nginx Configure the virtual host _nginx_02

windows Let's designate hosts

C:\Windows\System32\drivers\etc     Find this path , There's one down there hosts   Add... To it  IP Address and virtual host address 

     
  • 1.

Now Web search virtual machine host address and IP The address is accessible

These are the default virtual hosts

Default virtual host

vim /etc/nginx/conf.d/ In the first place is the default virtual host , By default, the first one is accessed ,

It can also be set manually , For example, copy a host file

cp default.conf 123.conf There are only two virtual hosts below , When accessing, the default access is the first 123.conf

nginx Configure the virtual host _ The configuration file _03
vim default.conf Edit the file in 80 Add later default_server As shown in the figure below , At this time, when visiting , It will not access the first place by default 123.conf, It's about visiting this default.conf The default host for

nginx Configure the virtual host _ Virtual host _04
vim 123.conf Modify according to the following figure When the changes are complete Don't forget to use nginx -t Check to see if you can change the error , then nginx -s reload Reload profile

nginx Configure the virtual host _linux_05

mkdir /tmp/html       The path modified according to the configuration file , Create a directory 
echo 123456 > /tmp/html/index.html      Create a file and import the contents 

     
  • 1.
  • 2.

Then visit... At this time www.ccc.com You can access 123456 The content of

Pan analysis

In order to restrict other domain names from being resolved to their own servers , For example, your domain name is abc.cn

Make one *.abc.cn The secondary domain name of A The record points to 222.222.222.222, Then it takes effect when the visitor enters “123.abc.cn” still “123.123.abc.cn” It could even be “ [[email protected]#.$%$.6543.ww.a.abc.cn](http://oapage.xmisp.com/help/#.$%$.6543.ww.a.abc.cn) ” Such arbitrary characters , Can point to 222.222.222.222 This IP Address . At this time, you need to disable the default virtual host

Add a line to the configuration file As shown in the figure deny all then nginx -s reload Reload profile

nginx Configure the virtual host _ The configuration file _06
At this point, accessing the default virtual host address again 403 No access .

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://cdmana.com/2022/174/202206231833088460.html

Scroll to Top