There are several ways to configure a virtual host in Nginx: You can add it directly to the Master profile (nginx.conf), or you can write a single file for all of your virtual hosts and include it in the main configuration section (include file name). The final approach is that each virtual host is a separate configuration file. This article takes the third method to do the demonstration
First make sure Nginx is installed to complete the next configuration file to create the virtual host and the directory where the virtual host files are located (my installation path is/usr/local/nginx)
Cd/usr/local/nginx/conf #切换到配置文件目录 mkdir vhosts #创建虚拟主机配置文件所在的目录 Touch vhosts/vhost1.conf #创建第一个虚拟主机的配置文件 |
Add the following line to this file:
server { Listen 80; # set the listening port; server_name www.test.com; # set host name; Location/{# defines the site and directory; Root/web/vhost/test1; # This directory will need to be created if it does not exist Index index.html; # This file also needs to be created in advance } Error_page 502 503 504/usr/local/nginx/html/50x.html; # define page status code for 5XX series error page Access_log/web/vhost/test1/logs/test1.access.log; # Set Access log path Error_log/web/vhost/test1/logs/logs/test1.error.log Crit; # Set Error log path }
|
If the above directory and file does not exist then follow the method to create
Mkdir/web/vhost/test/logs-p Touch/web/vhost/test/index.html Edit index.html file Add content and save to exit |
At this time the virtual host configuration file has been completed, but the restart Nginx service will not take effect because it is not included, open the nginx.conf file to add it to the last line of http{} segment, be sure to in the HTTP segment.
650) this.width=650; "src=" Http://i3.tietuku.com/9a36278fe0636009.png "width=" 214 "height=" "alt=" 9a36278fe0636009.png "/>
Because the test is used so open the browser may also not access to, then you can modify the native Hosts file to add the following:
IP (server IP address for Web server) hostname (the server_name we defined)
650) this.width=650, "src=" Http://i3.tietuku.com/140779a98e717833.png "width=" "height=," 119 "alt=" 140779a98e717833.png "/>
after all the settings can restart the Nginx server, if you do not add a service startup script, you can use Nginx-s quit && Nginx boot (if the Nginx sbin directory is not added to the environment variable, you need to use an absolute path to start it/usr/local/nginx/sbin/nginx-s quit &&/usr/local/nginx/sbin /nginx)
View the following access results
650) this.width=650; "src=" Http://i3.tietuku.com/8b6ce7e573f1f8b2.png "width=" 215 "height=" "alt=" 8b6ce7e573f1f8b2.png "/>
Next This virtual host to turn on the status monitoring function, in the virtual host configuration file to add a bit of content
location/status{ Stub_status on; #启动状态功能 Access_log off; #关闭访问日志 } |
Use NGINX-T to check whether the configuration file is normal, if it appears that the configuration file is normal, you can restart the Nginx service
650) this.width=650; "src=" Http://i3.tietuku.com/fb78d27d4ee66ad1.png "width=" 543 "height=" "alt=" Fb78d27d4ee66ad1.png "/>
then enter Www.test.com/status in the browser See if the status page appears
650) this.width=650; "src=" Http://i3.tietuku.com/c282116729fdca87.png "width=" 273 "height=" "alt=" C282116729fdca87.png "/>
Active Connections 2, accept 5 requests to process 5 Request Responses 64 (I have been refreshing: Read 0 Write 1 wait for one
In general, these status pages are not allowed to be accessed by unauthorized users, so we need to set up user authentication and add the following on the status page of the virtual host:
Auth_basic "admin"; AUTH_BASIC_USER_FILE/WEB/VHOST/TEST1/.PASSWD; |
650) this.width=650; "src=" Http://i3.tietuku.com/bacaf16a69b45f9b.png "width=" 365 "height=" "alt=" Bacaf16a69b45f9b.png "/>
The next generation of the. passwd file in the/web/vhost/test1/directory is generated in the following two ways
A. Using the HTPASSWD tool to generate directly
Htpasswd-c-m/web/vhost/test1/.passwd # htpasswd tool for httpd if not available please install Yun Imstall http D-tools |
B. Manually create the file after using OpenSSL encryption and add
OpenSSL Passwd-apr1-salt ' echo $RANDOM ' touch/web/vhost/test1/.passwd |
The format of this file is:
650) this.width=650; "src=" Http://i3.tietuku.com/88787eae7ea1fa8d.png "width=" 365 "height=" "alt=" 88787eae7ea1fa8d.png "/>
With Nginx-t check, if the profile is okay then reread the profile nginx-s reload and then open the browser to try to access
650) this.width=650; "src=" Http://i2.tietuku.com/547af1688a61d081.png "width=" 629 "height=" 290 "alt=" 547af1688a61d081.png "/>
This article is from the "Yxn" blog, make sure to keep this source http://jyxnt.blog.51cto.com/9581167/1596949
Nginx Virtual Host Configuration