Nginx身份验证并重定向到https

时间:2014-01-31 12:36:24

标签: nginx

我很难尝试为网站添加一些安全性, 基本上我希望目录下的内容必须:

1) be redirected to https if http
2) be under HttpAuthBasicModule

由于某些原因,我可以让auth工作,但它不会重定向到某些网址的https,例如/index.php,但它会用于其他一些文件:

/revive/www/admin/assets/images/login-welcome.gif works
/revive/www/admin/index.php remains under port 80

这是我的nginx配置文件的相关部分

location ^~ /revive/www/admin  {

    if ($server_port = 80) {
        rewrite ^ https://$host$request_uri permanent;
    }

    auth_basic            "Restricted";
    auth_basic_user_file  htpasswd_revive;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass    127.0.0.1:9001;
        fastcgi_param PHP_VALUE "newrelic.appname=revive.host.com";
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

如果在端口80上调用/ revive / www / admin下的所有内容都被重定向到端口443,我该如何确保?任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

仅将您的服务器规则与“listen 443”一起使用,并在下面添加以下规则:

server {
  listen 80;
  server_name mysite.com;
  rewrite ^ https://$server_name$request_uri? permanent;
}

答案 1 :(得分:0)

如果我这样做,它似乎正在起作用:

location ^~ /revive/www/admin  {

    if ($server_port = 80) {
        rewrite ^ https://$host$request_uri permanent;
    }

    auth_basic            "Restricted";
    auth_basic_user_file  htpasswd_revive;

    location ~ \.php$ {

        if ($server_port = 80) {
            rewrite ^ https://$host$request_uri permanent;
        }

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass    127.0.0.1:9001;
        fastcgi_param PHP_VALUE "newrelic.appname=revive.host.com";
        fastcgi_index index.php;
        include fastcgi_params;
    }
}