NGINX http到https如果只包含某个目录

时间:2013-11-15 15:17:55

标签: nginx

我的任务是完成一个项目。 我们的服务器上有两个目录,一个是 http://example.com/app 另一个是 http://example.com/fw

我被要求做的是,如果有任何访问者登陆这两个目录中的页面(app和fw),则从http重定向到https 这是我到目前为止在配置文件中所做的。当我将行添加到'server'下面的配置文件中并重新启动时,该站点将不会恢复。不幸的是我没有访问日志文件。感谢任何愿意看看这个的人

location ~ ^/miner/memclub/.+\.html$ {
    rewrite ^(.+)\.html$ /bootstrap.php?file=$1.html last;
    rewrite ^(.+)\.phtml$ /bootstrap.php?file=$1.phtml last;
    error_page 404 = /404.php;
}

server {
server_name site.org;
server_name *.site.org;
location /app {
     if ( $scheme = http ) {
     rewrite ^ https://site.org/app last;
     }
  }
}

1 个答案:

答案 0 :(得分:3)

首先,我认为你不能拥有2 server_name,将这两行合并为一行

server_name example.com *.example.com;

要进行https重定向,我建议使用2个独立的服务器,无论如何都需要一个监听端口443

server {
    server_name example.com www.example.com; # which ever you are using
    listen 443 ssl;
    location / {
        # all your https configuration
    }
}
server {
    server_name example.com www.example.com:
    listen 80;
    location /app {
        return 301 https://$http_host$request_uri;
    }
    location /fw {
        return 301 https://$http_host$request_uri;
    }
    location / {
        # the rest of the non https configuration
    }
}

我知道你可以将appfw合并到一个位置,但我相信没有正则表达式的做法会更快,如果你想在这里做到这一点

location /(app|fw) {
    return 301 https://$http_host$request_uri;
}