Maison > développement back-end > tutoriel php > nginx配置虚拟主机vhost

nginx配置虚拟主机vhost

WBOY
Libérer: 2016-08-08 09:19:51
original
1248 Les gens l'ont consulté

首先谈一点自己的理解

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

下面来说一下nginx环境下虚拟主机的配置

假设我们需要配置的虚拟主机域名为mail.zjc.com


虚拟主机存放目录为/var/www/mail.zjc.com/web
sudo mkdir -p /var/www/mail.zjc.com/web


创建nginx虚拟主机配置文件
sudo gedit /etc/nginx/sites-available/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>
Copier après la connexion

因为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>
Copier après la connexion

重新加载nginx使配置生效
sudo /etc/init.d/nginx reload


好了,接下来在/var/www/mail.zjc.com/web目录下创建一些文件(例如index.php)就可以通过http://mail.zjc.com/index.php来访问了。BTW,记得在hosts中或者DNS上设置mail.zjc.com指向nginx所在的IP地址。

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了nginx配置虚拟主机vhost,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal