![Nginx 重写多个位置](https://linux22.com/image/592621/Nginx%20%E9%87%8D%E5%86%99%E5%A4%9A%E4%B8%AA%E4%BD%8D%E7%BD%AE.png)
我想重写特定位置,例如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;
}