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;
}
您可以將您的代碼添加到我的原始代碼並請給我看? – MKK
,它是否覆蓋了類似'www.sample.jp/test1/test2'的URL?它是否需要'sample.jp/test1/test2'? – MKK
感謝您的更新。但是現在它重定向到了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