Nginx 配置多站點vhost


假設你想在Linux Nginx中用不同的域名訪問不同的目錄,這時就要配置多個vhost,具體配置如下,假設網站根目錄設定在/var/www/

 

1、在/var/www/下新建兩個目錄

/var/www/ushark.net
/var/www/ushark.wang

 

2、編輯/etc/nginx/nginx.conf

復制代碼
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;   #主要是加入此行,如有則忽略
}
復制代碼

 

3、在/etc/nginx/conf.d下新建兩個conf文件,

/etc/nginx/conf.d/ushark.net.conf
/etc/nginx/conf.d/ushark.wang.conf

 

4、復制如下配置信息到兩個文件中,只要修改紅色部分內容  !!! server_name與root保持一致即目錄和域名一一對應 !!!

復制代碼
server {
    listen       80;
    server_name   www.ushark.net;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    root   /var/www/ushark.net/;
    if (!-e $request_filename){    # rewrite可根據網站需要增刪
            rewrite ^/(.*) /index.php last;  
    }  

    location / {
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/ushark.net/;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~* \.php$ {
        fastcgi_index   index.php;
        fastcgi_pass    127.0.0.1:9000;
        include           fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
復制代碼

 

5、重啟Nginx

systemctl restart nginx

 

6、 編輯/etc/hosts  !!! 核心步驟 !!!

[root@bogon ~]# vi 127.0.0.1        localhost.localdomain localhost
::1              localhost6.localdomain6 localhost6
127.0.0.1        www.ushark.net
127.0.0.1        www.ushark.wang

 

7、設置成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM