404错误,尽管'location = /url'指令在nginx.conf中

mcdcgff0  于 4个月前  发布在  Nginx
关注(0)|答案(1)|浏览(64)

问题的简短版本:
我在我的nginx.conf和PHP脚本设置为接受和理解什么是需要从它,然而,当我去/sitemap.txt我得到一个404错误,指出“请求的资源/sitemap. txt没有找到在这台服务器上。”

location = /sitemap.txt { rewrite /.*$ /index.php; }

字符串
较长版本:
完整的nginx.conf是:

worker_processes  1;

error_log       /home/logs/nginx_error.log error;
pid         /home/tmp/nginx.pid;

events
{
    worker_connections  1024;
}

http
{
    client_max_body_size    19M;
    client_body_temp_path   /home/tmp;
    proxy_temp_path     /home/tmp;
    fastcgi_temp_path   /home/tmp;
    uwsgi_temp_path     /home/tmp;
    scgi_temp_path      /home/tmp;
    
    access_log      /home/logs/nginx_access.log;
    
    include mime.types;
    default_type  application/octet-stream;

    port_in_redirect on;
    
    sendfile    on;
    
    keepalive_timeout  65;
    
    server {
        listen      8000;
        server_name www.MYSERVER.COM;
        
        charset utf-8;
        
        location =/
        {
            absolute_redirect on;
            port_in_redirect on;
            server_name_in_redirect on;
            return 301 http://www.MYSERVER.COM:80/home;
        }
        location =/index.php
        {
            default_type text/html;
            
            proxy_pass  http://127.0.0.1:9000$request_uri;
            proxy_redirect  off;
            include     proxy_params;
        }
        location /uploads/
        {
            root /home/public;
            try_files $uri =404;
            expires 30d;
        }
        location /
        {
            rewrite ^/(?<pagename>.*)$ /index.php;
        }
        location = /sitemap.txt { rewrite /.*$ /index.php; }
        
        location /classes/ { deny all; }
        location /config/ { deny all; }
        
        #allow specific files
        location = /styles.css { root /home/public/resources; }
        location = /script.js { root /home/public/resources; }
        location = /robots.txt { root /home/public/resources; }
        location = /sitemap.xml { root /home/public/resources; }
        location = /favicon.webp { root /home/public/resources; }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }
    }

}


实际上,我不认为我甚至需要location = /sitemap.txt { rewrite /.*$ /index.php; }在那里,因为location /指令应该已经做的伎俩,但我想绝对肯定。
我确信指令可以运行。如果我把类似return 403;的东西放在那里,同样的代码会发送到浏览器。
我知道可以只将其设置为静态文件,但我想自动化一个过程

v440hwme

v440hwme1#

好吧,我已经发现了这个问题,并将在这里张贴,希望有人会发现它有用.问题是,虽然Nginx是正确的一切,这是PHP已经决定发挥不公平,并开始解析扩展内的URL.
对我来说,最简单的解决办法是改变,

location =/sitemap.txt { proxy_pass http://127.0.0.1:9000/sitemaptext; }

字符串
然后在我的PHP脚本中添加一条规则,接受该URL而不是正确的URL。这并不重要,因为外部世界并不知情,他们仍然会在地址中看到'.txt'。
我真的希望有一种方法可以做到这一点没有这样的黑客。

相关问题