iT邦幫忙

0

Nginx 子域名無法生效 -subdomain

各位好:
我目前想達成2種效果,第1點已經完成,
但第2點設置的子域名卻一直無法啟用,
再請各位給予指點,感謝。

1.non-www一律轉到有WWW => 此功能有作用!!
demo.com => www.demo.com
2.任意配置的子域名可導引到該網站下的相對應子目錄=> 無法作用
blog.demo.com => www.demo.com/blog/
但是我是想做這樣的功能:
blog.demo.com => www.demo.com/index.php?subdomain=blog
xx.demo.com => www.demo.com/index.php?subdomain=xx
..
3.我在搜尋歷史問題時,有看到 有這種特殊網址,
www.blog.demo.com / www.news.demo.com ....
這種也是可以利用subdomain方式達到的嗎?

nginx.conf基本上這個設定沒太大問題
/etc/nginx/nginx.conf

http {

    ....部分略....

    include /etc/nginx/conf.d/*.conf;
    # include 虛擬主機的配置文件

    index   index.html index.htm;

    server {
	if ( $host ~* "\d+\.\d+\.\d+\.\d+" ) {
	}

        listen       80 default_server;
        server_name  localhost;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.html {
        }

    }
}

以下這個就無法達到想要的效果配置:
/etc/nginx/conf.d/demo.conf

# 2015/01

server {
  #Redirect non-www to WWW
  listen       80;
  server_name  demo.com;
  return       301 http://www.demo.com;
}

server {
  listen       80;
  server_name  www.demo.com *.demo.com;

# ↓↓↓↓以下是子網域配置,但是無法生效?? ↓↓↓↓
#if ( $host ~* (\b(?!www\b)\w+)\.\w+\.\w+ ) {
#  set $subdomain /$1;
#}
#set $root_path '/usr/share/nginx/html/demo.com/$subdomain';
#  ↑↑↑↑ 以下是子網域配置,但是無法生效??


#以下都正常,通過測試

    set $root_path '/usr/share/nginx/html/demo.com';
    root $root_path;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?q=$uri\&;
    }
    location ~ \.php$ {
        try_files $uri $uri/ /index.php?q=$uri\&;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
	fastcgi_split_path_info ^(.+\.php)(.*)$;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_intercept_errors on;
    }
}
isthome iT邦新手 4 級 ‧ 2015-01-28 14:11:05 檢舉
其實後來發現這些nginx設定都是可以運作的,就差在網址商那邊的設定。
要多加上*.domain
isthome iT邦新手 4 級 ‧ 2015-01-28 14:11:05 檢舉
其實後來發現這些nginx設定都是可以運作的,就差在網址商那邊的設定。
要多加上*.domain
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

我要發表回答

立即登入回答