0

我设法通过以下方式将所有 /admin 请求重定向到 https:

server {
    listen 80;

    location /admin {
        rewrite ^ https://$server_name$request_uri?$args permanent;
    }
}

但是非 /admin https 请求需要重定向回 http。以下代码在重定向方面有点工作,但 /admin 返回 404。在空块中需要什么来保持 nginx 快乐?

server {
    listen       443;
    server_name  mywebsite.local.com;

    ssl                  on;
    ssl_certificate      /opt/nginx/server.crt;
    ssl_certificate_key  /opt/nginx/host.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    root /Users/me/Sites/website/public;

    location /admin {
        # here is your configuration for admin requests
        # don't forget to set "root"
    }

    location / {
        return 301 http://$server_name$request_uri;
    }

    passenger_enabled on;
    rails_env development;
}

error.log 中的错误:

12 open() "/Users/me/Sites/website/public/admin" failed (2: No such file or directory),     client: 127.0.0.1, server: something.com, request: "GET /admin HTTP/1.1", host: "something.com"

谢谢

4

2 回答 2

2
server {
    listen 80;

    location /admin {
        return 301 https://$server_name$request_uri;
   }

    location / {
        # here is your configuration for non-admin requests
        # don't forget to set "root"
    }
}

server {
    listen 443;

    location /admin {
        # here is your configuration for admin requests
        # don't forget to set "root"
    }

    location / {
        return 301 http://$server_name$request_uri;
    }
}
于 2012-09-13T07:55:03.930 回答
1

这似乎可行:

    if ($request_uri !~* ^/admin)
    {
        rewrite ^ http://$server_name$request_uri?$args permanent;
    }

有没有人有更好的方法?

于 2012-09-13T03:48:34.157 回答