Configure a virtual host based on domain name resolution
1. Prepare the site
Our site unified into the/www/vhosts/, each site root directory name is the same as the domain name, specifically as follows.
Create a new www.stu31.com site root directory
[Email protected] extra]# mkdir/www/vhosts/www.stu31.com
Homepage of new WWW site index.html
[Email protected] extra]# echo "welconf to www.stu31.com" >/www/vhosts/www.stu31.com/index.html
New bbs.stu31.com Site root directory
[Email protected] extra]# mkdir/www/vhosts/bbs.stu31.com
New BBS website homepage index.html, content such as welconf to bbs.stu31.com
[Email protected] extra]# echo "welconf to bbs.stu31.com" >/www/vhosts/bbs.stu31.com/index.html
New log file directory
# mkdir-p/var/logs/nginx
We unified the log into the/var/logs, this side is to store the Nginx log, so the Nginx log is kept in the current Nginx directory. Log Unified storage is relatively standard (if you're not used to it, you can do it your way)
2. Configure Nginx Virtual Host
Add nginx master configuration file nginx.conf
Configure the Nginx log format first, find the following in nginx.conf, and remove the # comment flag
#log_format Main ' $remote _addr-$remote _user [$time _local] "$request" ' # ' $status $body _bytes_sent "$h Ttp_referer "' # '" $http _user_agent "" $http _x_forwarded_for "'; Log_format main ' $remote _addr-$remote _us ER [$time _local] "$request" $status $body _bytes_sent "" $http _referer "" $http _user_agent "';
To add a virtual host configuration file:
Comment out the server segment first;
Include extra/nginx-vhost.conf;
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/97/wKioL1SfxHmQFYAuAABhlSxBP5A072.jpg "title=" 2.jpg " alt= "Wkiol1sfxhmqfyauaabhlsxbp5a072.jpg"/>
3. Configure the virtual host configuration file:
[[email protected] ~]# vim /etc/nginx/extra/nginx-vhost.conf server { listen 80; server_name www.stu31.com; index index.html index.htm; root /www/vhosts/www.stu31.com; access_log /var/log/nginx/www.stu31.com-access.log main; location / { }}server { listen 80; server_name bbs.stu31.com; index index.html index.htm; root /www/vhosts/ Bbs.stu31.com; access_log /var/log/nginx/bbs.stu31.com-access.log main; location / { }}
Configuration explanation
server{}: Configuring the virtual host must have this segment. SERVER_NAME: The domain name of the virtual host, you can write multiple domain names, similar to aliases, such as you can configure server_name www.stu31.com web.stu31.com. In this case, access to any domain name, the content is the same listen 80, listen to the IP and port, this side only the port, indicating the current server all IP 80 port, if only want to listen to 127.0.0.1 80, the following wording: Listen 127.0.0.1:80root/www/vhosts/ www.stu31.com: Site root directory, where your site files are stored. Note: Site directory and domain name as much as possible, develop a good habit Access_log/var/log/nginx/www.stu31.com-access.log main: Access log location/{} default URI
4. Restart the Nginx service and open the site
nginx-t Check the Nginx configuration is OK, the command is as follows :
[Email protected] ~]# nginx-tnginx:the configuration file/etc/nginx/nginx.conf syntax is oknginx:configuration file/ Etc/nginx/nginx.conf Test is successful
If you see the above two lines OK and successful to indicate the configuration problem, then we start the Nginx
Start Nginx
[Email protected] ~]# service nginx restartnginx:the configuration file/etc/nginx/nginx.conf syntax is Oknginx:configu Ration file/etc/nginx/nginx.conf test is successfulstopping nginx: [OK]sta rting nginx: [OK]
Access to www.stu31.com,bbs.stu31.com (my side of DNS has been resolved to 172.16.31.40, in the case of testing, we can through the version of the hosts), the binding host method is as follows:
The following content is added to C:\Windows\System32\Drivers\etc\hosts
172.16.31.40 www.stu31.com
172.16.31.40 bbs.stu31.com
The above is the windows binding hosts way, as follows is the Linux method
[Email protected] ~]# cat/etc/hosts127.0.0.1 www.stu31.com localhost localhost.localdomain localhost4 localhost4.loca ldomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6172.16.0.1 server.magelinux.com ser ver172.16.31.40 www.stu31.com172.16.31.40 bbs.stu31.com
Use a browser to access these two sites. My side uses curl to access it.
[[email protected] ~]# Curl http://www.stu31.comWelconf to www.stu31.com[[email protected] ~]# Curl/HTTP/ Bbs.stu31.comWelconf to Bbs.stu31.com
This article is from "Dragon Guardian" blog, please make sure to keep this source http://sohudrgon.blog.51cto.com/3088108/1596948
Nginx (6)-Configuring a domain-based virtual host