0

我刚刚从 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]

然后这是我当前的confNginx

etc/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;
}
4

1 回答 1

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会将原始 URL 中的所有内容附加到新 URL 中,例如

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

于 2013-10-22T04:48:39.233 回答