Nginx Learning note Port-based virtual host hostname-based virtual host root, alias, index configuration experimental Environment:centos test node ip:172.16.3.101 Port-based virtual host: vim /etc/ nginx/nginx.conf# to the inside of the http {} add the following content server {# server define a virtual host listen 8080;# Monitor all IP ports on this machine 8080 server_name www.test.com;# the virtual host name is:www.test.com location / {# depends on the URI of the user request to match the location, and if it matches, it will be processed by the configuration in that location root "/web/htdocs"; # web Resource Path mapping } } # Save exit # Create directory/web/htdocs mkdir -pv / web/htdocs vim /web/htdocs/index.html # Add the following content to the inside hello,my serser_name is www.test.com,port is 8080 # Save Exit # test, enter:http://172.16.3.101 separately in remote browser and http://172.16.3.101:8080 # if the corresponding result is displayed, the port-based virtual host configuration is successfully configured for host-name-based virtual host vim /etc/nginx/nginx.conf# to the inside of the http{} Add the following content server {# server define a virtual host listen 80;# Monitor all IP ports on this machine: 80 server_name www.test.com;# The virtual host name is:www.test.com location / {# Depending on the rul of the user request, the location is matched, and if it matches, it will be processed by the configuration in that location root "/web/htdocs"; # web Resource Path mapping } } server {# server define a virtual host listen 80;# Monitor all IP ports on this machine: 80 server_name mail.test.com;# Virtual host named:mail.test.com location / {# above already said, Do not repeat here      &NBsp; root "/web/mail"; # web Resource Path mapping } }mkdir -pv /web/mailvim /web/mail/index.html# Add the following content to it hello,my server_name is mail.test.com# Save Exit vim /web/htdocs/index.html# Add the following content to it hello,my server_name is www.test.com# Save exit # check its syntax [[email protected] conf]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful# Restart Nginx "You need to restart Nginx because the port number is modified here" [[Email protected] conf]# service nginx restartnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulstopping nginx: [ OK ]Starting nginx: [ OK ]# Config parsing file/etc/hosts "here to access the test or the local, so I'll modify the native Hosts file" vim /etc/hosts# Add the following content to the 172.16.3.101 www.test.com# to parse the hostname www.test.com to 172.16.3.101172.16.3.101 mail.test.com # resolve hostname mail.test.com to 172.16.3.101# save exit # test " In this machine direct test on line, previously said, the Hosts file has been modified "# test www.test.com virtual host [[email protected] conf]# elinks - Dump http://www.test.com hello,my server_name is www.test.com # , www.test.com virtual host normal # test mail.test.com Virtual host [[email protected] conf]# elinks -dump http:// mail.test.com hello,my server_name is mail.test.com # , Mail.test.com Virtual Host the difference between normal root and alias: I do not say, and see the following example vim /etc/nginx/nginx.conf# add the following statement http{} inside server { listen 80; server_name www.test.com; location /root { root "/web/htdocs"; } } server { listen 80; server_name mail.test.com; location /alias { alias "/web/mail"; } }# For virtual host www.test.commkdir -pv /web/htdocs/root/vim /web/htdocs/root/index.html# Add the following statement to the inside hello,this is root type.# save, exit # for the virtual host Mail.test.commkdir -pv /web /mailvim /web/mail/index.html# Add the following statement to the inside hello,this is alias type.# save Exit # Syntax Check Nginx -t# nginx service Restart service nginx restart # configuration parsing file/etc/hosts, as the previous experiment did, There's no repetition here. # Test "Direct in native test" # test www.test.com virtual host [[Email protected] root]# elinks -dump http://www.test.com/root/ hello,this is root type.# the root type is accessed like this, This works, and its corresponding access path is:/web/htdocs/root/index.html, I don't need to say more. # test mail.test.com Virtual Host [[email protected] root]# elinks -dump http://mail.test.com/alias hello,this is alias type.# the alias type is accessed in this way, This works, and its corresponding access path is:/web/mail/index.html, I don't have to explain it. Index configuration: Set the default home page look at the following action vim /etc/nginx/nginx.conf# put the following into the inside server { listen 80; server_name www.test.com; location /root { root "/web/htdocs"; } }# nginx Syntax Checker nginx -t # nginx service restart nginx restart # create its Web resource path and file mkdir -pv /web/htdocs/root/vim /web/htdocs/ root/index.html# Add the following statement to the inside hello,this is root type.# save, Exit # configuration parsing file/etc/hostsvim /etc/hosts# Add the following statement in which 172.16.3.101www.test.com# save exit # access "on-line test access" # Into the following statement, there is no home page file, but also access to normal, the Nginx default home file is Index.html[[email protected] root]# elinks -dump http://www.test.com/root/ hello,this is root type.vim /etc/nginx/ nginx.conf# Replace the statement you just added with the following statement server { listen 80; server_name www.test.com; location /root { root "/web/htdocs"; index test.html } }# nginx Grammar Check nginx -t# nginx Service Restart service nginx restart# access "on the local test access on the line" # enter the following statement, where the home page file is not entered, but does not have access to the [ [email protected] root]# elinks -dump http://www.test.com/root 403 Forbidden -------------------------- ------------------------------------------------ nginx/1.6.2# now I'll change the page file name mv /web/htdocs/root/index.html / web/htdocs/root/test.html# again visit # enter the following statement, where the home page file is not entered, you can access the normal [[email protected] root]# elinks -dump http://www.test.com/root hello,this is root Type. Now you should understand the role of index.
This article is from the "You Devil" blog, please be sure to keep this source http://599184429.blog.51cto.com/5147223/1596587
Nginx Learning Notes Port-based virtual host host name-based virtual host root, alias, index configuration