Home >Backend Development >PHP Tutorial >nginx configure virtual host vhost
First talk about my own understanding
所谓虚拟主机,是说通过几个不同的url地址,都能到达nginx环境,只不过针对不同的url,处理的逻辑不同。 nginx支持虚拟主机,但是浏览器等客户端不知道,所以虚拟主机的几个地址,应该是都指向nginx所在的ip地址,虚拟主机功能才能正常。
Let’s talk about the configuration of the virtual host in the nginx environment
Assume that the virtual host domain name we need to configure is mail.zjc.com
The virtual host storage directory is /var /www/mail.zjc.com/web sudo mkdir -p /var/www/mail.zjc.com/web
Create nginx virtual host configuration filesudo gedit /etc/nginx/sites-available /mail.zjc.com.vhost
The content is (where the php domain is configured according to the local PHP application)
<code><span>server</span> { <span>listen</span><span>80</span>; <span>server_name</span> mail.zjc.com; <span>root</span> /var/www/mail.zjc.com/web; <span>if</span> (<span>$http_host</span> != <span>"mail.zjc.com"</span>) { <span>rewrite</span><span> ^</span><span>http://mail.zjc.com$request_uri</span><span>permanent</span>; } <span>index</span> index.php index.html; <span>location</span> = /favicon.ico { <span>log_not_found</span><span>off</span>; <span>access_log</span><span>off</span>; } <span>location</span> = /robots.txt { <span>allow</span> all; <span>log_not_found</span><span>off</span>; <span>access_log</span><span>off</span>; } <span># 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!</span><span>location</span><span>~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_</span> { <span>deny</span> all; } <span># Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).</span><span>location</span><span>~ /\.</span> { <span>deny</span> all; <span>access_log</span><span>off</span>; <span>log_not_found</span><span>off</span>; } <span>location</span><span>~* \.(jpg|jpeg|png|gif|css|js|ico)$</span> { <span>expires</span> max; <span>log_not_found</span><span>off</span>; } <span>location</span><span>~ \.php$</span> { <span>try_files</span><span>$uri</span> =<span>404</span>; <span>fastcgi_split_path_info</span><span> ^(.+\.php)(/.+)$</span>; <span>fastcgi_pass</span><span>unix:/var/run/php5-fpm.sock</span>; <span>fastcgi_index</span> index.php; <span>fastcgi_param</span> SCRIPT_FILENAME <span>$document_root</span><span>$fastcgi_script_name</span>; <span>include</span> fastcgi_params; } }</code>
Because nginx’s main configuration file nginx.conf will automatically apply the configuration file in sites-enabled, so we need to create one in sites-enabled Link to mail.zjc.com.vhost file
<code>cd /etc/nginx/sites-enabled sudo ln -s /etc/nginx/sites-available/mail<span>.zjc</span><span>.com</span><span>.vhost</span> www<span>.zjc</span><span>.com</span><span>.vhost</span></code>
Reload nginx to make the configuration take effectsudo /etc/init.d/nginx reload
Okay, next is /var/www/mail. Create some files (such as index.php) in the zjc.com/web
directory and then access them through http://mail.zjc.com/index.php
. BTW, remember to set mail.zjc.com in hosts or DNS to point to the IP address of nginx.
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces nginx to configure the virtual host vhost, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.