Nginx 重写多个位置

Nginx 重写多个位置

我想重写特定位置,例如https这里:

server {
    listen       80;
    server_name  mysite.com www.mysite.com;
    root   /var/www/$server_name/public;
    #error_log /error.logg debug;
    error_page 403 /403.html;
    location  = /403.html {
        allow all;
    }
    location / {
        #allow xxx.xxx.xxx;
        #deny all;
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location /register {
        rewrite ^ https://$http_host$request_uri?$args permanent;
    }
    location /login {
        rewrite ^ https://$http_host$request_uri?$args permanent;
    }
}

/register并且/login不是唯一需要写入的地方,所以我该如何处理多个位置而不必为每个想要重写为 https 的地方重复位置块?

答案1

您可以使用正则表达式位置匹配,如下所示:

location ~ ^/(login|register) {
    rewrite ^ https://$http_host$request_uri?$args permanent;
}

相关内容