nginx設定虛擬主機vhost

WBOY
發布: 2016-08-08 09:19:51
原創
1232 人瀏覽過

先談一點自己的理解

   所谓虚拟主机,是说通过几个不同的url地址,都能到达nginx环境,只不过针对不同的url,处理的逻辑不同。
    nginx支持虚拟主机,但是浏览器等客户端不知道,所以虚拟主机的几个地址,应该是都指向nginx所在的ip地址,虚拟主机功能才能正常。
登入後複製

下面來說一下nginx環境下虛擬主機的配置

假設我們需要配置的虛擬主機網域為mail.zjc.com


虛擬主機目錄為/var /www/mail.zjc.com/web
sudo mkdir -p /var/www/mail.zjc.com/web


建立nginx虛擬主機設定檔
sudo gedit /etc-nginablex/et /mail.zjc.com.vhost
內容為(其中php域根據本機php應用情況配置)

<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>
登入後複製

因為nginx的主設定檔nginx.conf會自動套用sites-enabled中的設定文件,所以我們需要在sites-enabled中建立一個mail.zjc.com.vhost檔案的連結

<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>
登入後複製

重新載入nginx使設定生效
sudo /etc/init.d/nginx reload


好了,接下來在/var/www/e. zjc.com/web目錄下建立一些檔案(例如index.php)就可以透過http://mail.zjc.com/index.php

來存取了。 BTW,記得在hosts中或DNS上設定mail.zjc.com指向nginx所在的IP位址。

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

以上就介紹了nginx設定虛擬主機vhost,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

🎜
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板