Nginx practices-Implementation of Virtual Hosts on Nginx

Source: Internet
Author: User
Tags rsyslog

Using the virtual host technology, you can divide a real host into many "virtual" hosts. Each virtual host has an independent domain name and IP address, and has a complete Internet server (www, FTP, email. Virtual Hosts are completely independent. in the outside world, each virtual host is exactly the same as an independent host. The effect is the same, but the cost is quite different. Because multiple virtual hosts share the resources of a real host, the hardware cost, network maintenance cost, and communication line cost of each virtual host user are greatly reduced, the Internet is truly an affordable network for everyone!

In the current production environment, most service providers use virtual hosts to provide web services to customers. virtual hosts include IP-based virtual hosts, port-based virtual hosts and name-based virtual hosts, because currently the most popular is name-based virtual hosts, that is, you can use the same port and IP address to correspond to multiple domain name sites, this experiment is based on this method.

1. Create site directories, home pages, and permissions

 
 
  1. [Root @ rhel6u3-7 ~] # Uname-r // view system kernel version 2.6.32-279. el6.i686 [root @ rhel6u3-7 ~] # Cat/etc/redhat-release // view system version Red Hat Enterprise Linux Server release 6.3 (Santiago) [root @ rhel6u3-7 ~] #

  2. [Root @ rhel6u3-7 nginx] # pwd

  3. /Usr/local/nginx

  4. [Root @ rhel6u3-7 nginx] # mkdir server sites // create server field configuration file directory as server, site home directory as sites

  5. [Root @ rhel6u3-7 nginx] # mkdir sites/www sites/www1 // create a sub-site directory in the site home directory

  6. [Root @ rhel6u3-7 nginx] # echo "This is www.rsyslog.org"> sites/www/index.html // create test Homepage

  7. [Root @ rhel6u3-7 nginx] # echo "This is www1.rsyslog.org"> sites/www1/index.html // create test Homepage

  8. [Root @ rhel6u3-7 nginx] # chown nginx. server/sites/-R // set the current owner and group to nginx


Ii. EditingNginxMaster configuration file, and addServerFields andLocationField. SetWww.rsyslog.orgAndWww1.rsyslog.orgTwo Virtual Hosts

 
 
  1. [Root @ rhel6u3-7 nginx] # vim conf/nginx. conf

  2. ......... // Add the server field to the http module, and then add the location field to the server field.

  3. Server {

  4. Listen 80; // set the listener port of the VM to 80.

  5. Server_name www.rsyslog.org; // sets the virtual host domain name


  6. Location /{

  7. Root sites/www; // set the relative path of the VM's home directory

  8. Index index.html index.htm; // sets the default homepage of a VM.

  9. }

  10. Location/status {// view the current nginx status, which requires the module "-- with-http_stub_status_module" Support

  11. Stub_status on;

  12. Access_log/usr/local/nginx/logs/www_status.log; // set the log storage location and name it

  13. Auth_basic "NginxStatus ";}


  14. }

  15. Include/usr/local/nginx/server/www1.rsyslog.org; // set the include statement to point to the location of the www1 Site server Field Configuration File

  16. ........


3. edit a websiteWww1.rsyslog.orgOfServerConfiguration File

 
 
  1. [Root @ rhel6u3-7 ~] # Cd/usr/local/nginx/server/

  2. [Root @ rhel6u3-7 server] # vim www1.rsyslog.org

  3. Server {

  4. Listen 80;

  5. Server_name www1.rsyslog.org;


  6. Location /{

  7. Root sites/www1;

  8. Index index.html index.htm;

  9. }

  10. Location/status {// view the current nginx status, which requires the module "-- with-http_stub_status_module" Support

  11. Stub_status on;

  12. Access_log/usr/local/nginx/logs/www1_status.log;

  13. Auth_basic "NginxStatus ";}

  14. }


4.DNSAdd twoARecord pointing to website host name

For DNS setup and configuration, see http://dreamfire.blog.51cto.com/418026/1091943

 
 
  1. // Add the records of two websites to the DNS region File

  2. Www A 192.168.100.107

  3. Www1 A 192.168.100.107


5. StartNginxService, in order to facilitate the test to disable the firewall andSelinuxSetPremissiveMode


 
 
  1. [root@rhel6u3-7 server]# /etc/rc.d/init.d/nginx restart

  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

  4. Stopping nginx: [ OK ]

  5. Starting nginx: [ OK ]

  6. [root@rhel6u3-2 ~]# /etc/rc.d/init.d/iptables stop

  7. [root@rhel6u3-2 ~]# setenforce 0


6. PassWindowsTo test the system, first set the network cardDNSSet192.168.100.102And thenNslookupWhether the command is successfully parsed.



PassIEBrowser access




AddStatusYou can view the current running status of the website



This article is from the blog of "the Linux open source technology blog", please be sure to keep this source http://dreamfire.blog.51cto.com/418026/1141018

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.