更新:
就我的情况而言,解决方案是这样的:
map $http_host$request_uri $pageCache {
default "nonexistent";
"~^(?<subdomain1>.{4})(?<subdomain2>.*)\.example\.com(?<folder1>.*?)\/?(\?.*)?$" page-cache/subdomains/$subdomain1/$subdomain1$subdomain2$folder1/1.html;
"~^example\.com(?<folder1>.*?)\/?(\?.*)?$" page-cache$folder1/1.html;
}
特别是这部分:
(?<folder1>.*?)\/?(\?.*)?
谢谢杰拉德·H·皮勒!
我如何才能从 URL 中获取除最后一个 /(正斜杠)和任何查询字符串(如 ?querystring=blah)之外的所有内容 - 我需要将其捕获到一个组中,例如下面的“路径”组。以下示例捕获了一个“路径”组,但如果最后一个字符不是 / 或 ,它将不起作用?
^(?<path>.*[^\/?])
以下将捕获所有内容,包括最后一个正斜杠(但之后没有任何内容),但我也需要省略最后一个正斜杠:
^(?<path>.*[\/])
例如我需要:
- https://subdomain.example.com 匹配 https://subdomain.example.com
- https://subdomain.example.com/ 匹配 https://subdomain.example.com
- https://subdomain.example.com?query 匹配 https://subdomain.example.com
- https://subdomain.example.com/?query 匹配 https://subdomain.example.com
- https://subdomain.example.com/folder 匹配 https://subdomain.example.com/folder
- https://subdomain.example.com/folder/ 匹配 https://subdomain.example.com/folder
- https://subdomain.example.com/sub/folders 匹配 https://subdomain.example.com/sub/folders
- https://subdomain.example.com/sub/folders/ 匹配 https://subdomain.example.com/sub/folders
- https://subdomain.example.com/sub/folders/?query 匹配 https://subdomain.example.com/sub/folders
- https://subdomain.example.com/sub/folders/?query=value 匹配 https://subdomain.example.com/sub/folders
如果你想知道为什么我需要这个正则表达式,那是因为我需要获取完整路径来确定“缓存的 html”文件的位置,而不是 php
map $request_uri $request_uri_path {
"~^(?<path>.*[^\/?])$" $path;
}
# Get the page cache path
map $http_host$request_uri_path $pageCache {
default "nonexistent";
"~^(?<subdomain1>.{4})(?<subdomain2>.*)\.example\.com(?<folder1>.*)$" page-cache/subdomains/$subdomain1/$subdomain1$subdomain2$folder1/1.html;
"~^example\.com(?<folder1>.*)$" page-cache$folder1/1.html;
}
注意:当我使用 $uri 时,它返回一个带有“/index.php”的值,这不是我想要的。我也不能使用 $scheme://$http_host,因为它不包含 url 的文件夹路径(例如 /sub/folders)。
附言:是的,我之前问过这个问题,但是我提出时没有做出适当的解释,所以我删除了它并重新提交,并进行了更详细的说明。
更新:根据要求全面封锁服务器:
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name .example.com;
root /home/sys/example.com/public;
# Block Bad Bots
if ($http_user_agent ~* (bingbot|360Spider|80legs.com) ) {
return 444;
}
# SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/example.com/123/server.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/123/server.key;
ssl_protocols TLSv1.2;
ssl_ciphers blahblah;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
include sys-conf/example.com/server/*;
location / {
limit_req zone=one burst=10 nodelay;
if ($http_user_agent ~* "^.*wkhtmltoimage.*$"){
return 403;
}
try_files $pageCache $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
location = /ads.txt {
access_log off;
log_not_found off;
}
location ~* \.(js)$ {
expires 3d;
}
access_log off;
error_log /var/log/nginx/example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
#tweaks
fastcgi_buffers 8 16k; # increase the buffer size for PHP-FTP
fastcgi_buffer_size 32k; # increase the buffer size for PHP-FTP
fastcgi_connect_timeout 60;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
答案1
请尝试一下:
map $uri $pageCache {
"~^(?<folder1>.*?)/?(\?.*)?$" page-cache$folder1/1.html;
}