nginx虛擬主機與二級網域對應問題
某草草
某草草 2017-05-16 17:17:54
0
2
615

使用nginx的時候,虛擬主機和二級網域對應問題,大家是怎麼做的,例如,有兩個二級網域,對應兩個資料夾:

域名         文件夹
111.aa.com   /var/www/111.aa.com
222.aa.com   /var/www/222.aa.com

那麼,在設定檔中,就對應兩個server
#111.aa.com

#
#111.aa.com
server {
    listen       80;
    server_name  111.aa.com;

    charset utf-8;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /var/www/111.aa.com;
        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   /usr/share/nginx/html;
    }

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


    location ~ \.php$ {
        root           /var/www/111.aa.com;
        fastcgi_pass   unix:/dev/shm/php-fpm.sock;
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        include        fastcgi_params;
    }

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

#222.aa.com

#222.aa.com
server {
    listen       80;
    server_name  222.aa.com;

    charset utf-8;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /var/www/222.aa.com;
        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   /usr/share/nginx/html;
    }

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


    location ~ \.php$ {
        root           /var/www/222.aa.com;
        fastcgi_pass   unix:/dev/shm/php-fpm.sock;
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        include        fastcgi_params;
    }

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

問題:
1、如果有100個二級域名,就得寫100個server,能否寫在1個server中呢,能否給個範例?
2、寫100個server和1個server,那種方式比較好呢?

某草草
某草草

全部回覆(2)
Peter_Zhu

如果是100個不同的項目, 當然要寫100個server(會存在很多定制的情況, 最常見的就是rewrite). 如果是有一個項目對應多個域名的情況, server數量可以減少一些. 即便有方法將100個項目的配置寫在一個server裡, 那配置文件我估計也會非常複雜, 管理起來會很頭疼. 倒不如寫100個server來的方便.

配置 多 不等於 複雜
配置 多 不等於 複雜
配置 多 不等於 複雜

"多" 從來不應該是配置管理擔心的事情.

左手右手慢动作

如果配置都差不多,可以用nginx的變數來判斷,這樣只要寫1個配置就可以了

可參考的內容:
http://bneijt.nl/blog/post/name-based-vi...
http://www.sitepoint.com/set-automatic-v...

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!