2013-10-21 51 views
0

我剛剛從Apache切換到Nginx。
然後我陷入了這個問題。
當我使用Apache時,可以通過使用這個.htaccess
自動刪除第一個www(sub-domain)如果我想在Nginx上使用相同的功能,我該怎麼做?如何在URL中包含www(子域名)?

這裏是我的老.htaccess

的.htaccess(我用的是這個,我使用Apache時)

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

RewriteCond %{HTTP_HOST} ^www\.(test-sample-site\.com)$ [NC] 
RewriteRule^http://%1%{REQUEST_URI} [R=301,L] 

然後,這裏是我的Nginx

等/ nginx的/當前的conf conf.d/rails.conf

upstream sample { 
    ip_hash; 
    server unix:/var/run/unicorn/unicorn_sample.sock fail_timeout=0; 
} 

server { 
    listen 80; 
    server_name sample.jp; 
    root /var/www/html/sample/public; 

    location /wiki { 
     alias /usr/share/wiki; 
     index index.php; 
    } 

    location ~ ^/wiki.+\.php$ { 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     fastcgi_split_path_info ^/wiki(.+\.php)(.*)$; 
     fastcgi_param SCRIPT_FILENAME /usr/share/wiki$fastcgi_script_name; 
     include   /etc/nginx/fastcgi_params; 
    } 


    location/{ 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header Host $http_host; 
     proxy_redirect off; 

     if (!-f $request_filename) { 
      proxy_pass http://sample; 
      break; 
     } 
    } 

    location ~ ^/assets|system/ { 
     expires 1y; 
     add_header Cache-Control public; 
     log_not_found off; 
    } 
} 

UPDATE

upstream sample { 
    ip_hash; 
    server unix:/var/run/unicorn/unicorn_sample.sock fail_timeout=0; 
} 

server { 
    listen 80; 
    server_name sample.jp; 
    root /var/www/html/sample/public; 

    location /wiki { 
     alias /usr/share/wiki; 
     index index.php; 
    } 

    location ~ ^/wiki.+\.php$ { 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     fastcgi_split_path_info ^/wiki(.+\.php)(.*)$; 
     fastcgi_param SCRIPT_FILENAME /usr/share/wiki$fastcgi_script_name; 
     include   /etc/nginx/fastcgi_params; 
    } 


    location/{ 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header Host $http_host; 
     proxy_redirect off; 

     if (!-f $request_filename) { 
      proxy_pass http://sample; 
      break; 
     } 
    } 

    location ~ ^/assets|system/ { 
     expires 1y; 
     add_header Cache-Control public; 
     log_not_found off; 
    } 
} 

server {   # <== redirection server block 
    server_name www.sample.jp; 
    return 301 sample.jp$request_uri; 
} 

回答

1

該服務器塊添加到您的配置(在底部或頂部,但其他服務器塊外)

server { 
    server_name www.sample.jp; 
    return 301 sample.jp$request_uri; 
} 

你可以把它添加到你的配置一樣這

upstream sample { # <== upstream block 
    # ... 
} 
server {   # <== server block 
    # .... 
} 
server {   # <== redirection server block 
    server_name www.sample.jp; 
    return 301 sample.jp$request_uri; 
} 

至於它會做什麼,$request_uri會追加在原來的網址到新的每一件事情,例如

http://www.example.com/a/b/c 

$request_uri = /a/b/c

新的URL將

http://example.com/a/b/c 

我相信它應該追加查詢字符串也是,?x=y部分http://example.com/a/b?x=y

+0

您可以將您的代碼添加到我的原始代碼並請給我看? – MKK

+0

,它是否覆蓋了類似'www.sample.jp/test1/test2'的URL?它是否需要'sample.jp/test1/test2'? – MKK

+0

感謝您的更新。但是現在它重定向到了URL http://www.sample.jp/sample.jp/sample.jp/sample.jp/sample.jp/sample.jp/sample.jp/sample.jp/.same當我嘗試訪問'http:// www.sample.jp'時,重複' – MKK

相關問題