【问题标题】:Nginx location block doesn't workNginx 位置块不起作用
【发布时间】:2017-06-16 12:58:40
【问题描述】:

我正在尝试将 nginx 服务器配置为以不同于所有其他请求的方式处理对 /awstats 的请求。我也为 /fpm-status 这样做了。但是,这个特定位置似乎不起作用。我已经尝试了位置匹配的所有排列。确切的问题是位置“/awstats”也被重写为php url。有人可以指出以下配置中的错误吗?

server {
listen 80;
listen 443 ssl;

ssl    on;
ssl_certificate    /etc/ssl/bundle.crt;
ssl_certificate_key    /etc/ssl/domain.key;

server_name domain.tld;

root   /var/www/html/project/;
index  index.php index.html index.htm;

location ^~ ^/awstats {
            root /var/www/awstats/domain.tld/;
            index awstats.domain.tld.html;
    }

    location ~ ^/awstats-icon/ {
            alias  /var/www/awstats/icon/;
    }

location ~* \.tpl|\.ini|\.log|(?<!robots)\.txt$ {
    deny all;
    return 404;
}

location ~ ^/fpm_status {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_pass php-fpm;
    allow 127.0.0.1;
    allow 43.252.193.74;
    allow 172.31.3.18;
    deny all;
}

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
            expires 365d;
    }

rewrite ^/sitemap.xml$ /index.php?route=feed/google_sitemap last;
rewrite ^/googlebase.xml$ /index.php?route=feed/google_base last;
rewrite ^/system/download/(.*) /index.php?route=error/not_found last;
if (!-f $request_filename){
    set $rule_3 1$rule_3;
}
if (!-d $request_filename){
    set $rule_3 2$rule_3;
}
if ($uri !~ ".*.(ico|gif|jpg|jpeg|png|js|css)"){
    set $rule_3 3$rule_3;
}

rewrite ^/api/((?!apidoc).*) /index.php?route=api/$1 last;
rewrite ^/admin_api/((?!adminapidoc).*) /admin/index.php?route=admin_api/$1 last;
rewrite ^/((?!admin|favicon|awstats|fpm_status|robots)(.*)) /index.php?_route_=$1 last;
location / {
    try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
        root /usr/share/nginx/html;
}

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;
}

access_log /var/log/nginx/domain.tld.access.log timed_combined;

}

【问题讨论】:

    标签: nginx


    【解决方案1】:

    您的位置语句似乎混淆了前缀位置和正则表达式位置语法:

    location ^~ ^/awstats 
    

    ^~ 修饰符与 前缀 位置一起使用以更改其评估顺序,但此处您已指定 正则表达式 参数。

    使用任一:

    location ^~ /awstats 
    

    或:

    location ~ ^/awstats 
    

    详情请见this document

    【讨论】:

    • 现在都试过了。可悲的是,要么没有工作。我什至尝试过完全匹配。那也没用。
    猜你喜欢
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 2019-08-27
    • 1970-01-01
    相关资源
    最近更新 更多