Configure the virtual host vhost in nginx

Source: Internet
Author: User
: This article describes how to configure the virtual host vhost in nginx. if you are interested in the PHP Tutorial, refer to it. First, let's talk about your own understanding.

The so-called virtual host means that the nginx environment can be reached through several different URLs, but the processing logic is different for different URLs. Nginx supports virtual hosts, but the browsers and other clients do not know. Therefore, several addresses of the virtual hosts should all point to the IP address of nginx, so that the functions of the virtual host can be normal.

The following describes the configuration of virtual hosts in the nginx environment.

Assume that the virtual host domain we want to configure is mail.zjc.com.

The VM storage directory is/var/www/mail.zjc.com/web.
sudo mkdir -p /var/www/mail.zjc.com/web

Create an nginx VM configuration file
sudo gedit /etc/nginx/sites-available/mail.zjc.com.vhost
The content is (where the php domain is configured based on the local php application)

server {       listen80;       server_name mail.zjc.com;       root /var/www/mail.zjc.com/web;       if ($http_host != "mail.zjc.com") {                 rewrite ^http://mail.zjc.com$request_uripermanent;       }       index index.php index.html;       location = /favicon.ico {                log_not_foundoff;                access_logoff;       }       location = /robots.txt {                allow all;                log_not_foundoff;                access_logoff;       }       # Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!location~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {                deny all;        }       # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).location~ /\. {                deny all;                access_logoff;                log_not_foundoff;       }       location~*  \.(jpg|jpeg|png|gif|css|js|ico)$ {                expires max;                log_not_foundoff;       }       location~ \.php$ {            try_files$uri =404;            fastcgi_split_path_info ^(.+\.php)(/.+)$;            fastcgi_passunix:/var/run/php5-fpm.sock;            fastcgi_index index.php;            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;            include fastcgi_params;        }}

Because the nginx main configuration file nginx. conf will automatically apply the configuration file in sites-enabled, we need to create a link to the mail.zjc.com. vhost file in sites-enabled.

cd /etc/nginx/sites-enabledsudo ln -s /etc/nginx/sites-available/mail.zjc.com.vhost www.zjc.com.vhost

Reload nginx to make the configuration take effect
sudo /etc/init.d/nginx reload

Now, in/var/www/mail.zjc.com/webDirectory to create some files (such as index. php) throughhttp://mail.zjc.com/index.php. BTW, remember to set mail.zjc.com to the IP address of nginx in hosts or DNS.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above describes how to configure the virtual host vhost in nginx, including some content, and hope to help anyone interested in the PHP Tutorial.

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.