禁用某些目录的规则

时间:2018-06-27 10:36:16

标签: nginx nginx-location

我正在使用以下规则:

if ($request_uri ~ ^/([^?]*)\.(php|html|htm)($|\?)) {  
    return 302 /$1?$args;  
}

删除URL的.php / .html / .htm扩展名,现在我想对诸如/site/ajax这样的目录禁用此规则。

这是我的完整服务器配置:

server {
    server_name www.example.net;
    listen 443;
    root   /var/www/html;
    index index.php;

    ssl on;
    ssl_certificate /path/cert.pem;
    ssl_certificate_key /path/privkey.pem;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location /site/ajax/ {
        index index.php;
        break;
    }

    #Remove .php | .html | .htm
    if ($request_uri ~ ^/([^?]*)\.(php|html|htm)($|\?)) {  
        return 302 /$1?$args;  
    }


    location / {
        try_files $uri $uri/ $uri.html $uri.php?$args;
    }

    location /site/query {
        try_files $uri $uri/ $uri.html $uri.php?$args;
        index manage.php; #Change index for query folder
    }

    error_page 404 /404.html;
    error_page 403 /403.html;

    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;
}

有可能吗?

1 个答案:

答案 0 :(得分:1)

一种解决方案是在正则表达式中使用否定的超前断言。例如:

^/(?!site/ajax)([^?]*)\.(php|html|htm)($|\?))