如果url包含www(子域),我如何取消它?

时间:2013-10-21 20:00:13

标签: nginx

我刚从Apache切换到Nginx 然后我就遇到了这个问题 当我使用Apache时,可以通过www(sub-domain)来自动删除第一个.htaccess 如果我想在Nginx上使用相同的内容,我该怎么做?

这是我以前的.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.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;
    }
}

更新

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 个答案:

答案 0 :(得分: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

新网址

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

我认为它也应附加查询字符串,?x=y中的http://example.com/a/b?x=y部分