将所有路径和文件调用重定向到 /index.php,如果未找到,使用 nginx。

huangapple go评论63阅读模式
英文:

Direct all path and file calls to /index.php if not found with nginx

问题

我试图使所有路径和文件请求都直接指向 /index.php,如果它们不存在。

当前根目录(/var/www/mysite.local/html/public/)只有一个文件(index.php),没有子目录。

目标是,除非路径和/或文件名存在,无论路径或文件名是什么,无论扩展名是什么(.php .jpg .gif .html .whatever),请求都应该指向 /index.php

目前,这仅适用于路径(/ 或 /something/or/other/)。

这是我在 /etc/nginx/sites-available/mysite.local 中的配置:

server {
    listen 80;
    root /var/www/mysite.local/html/public/;
    index index.php;
    server_name mysite.local;

    access_log /var/log/nginx/mysite_local.access.log;
    error_log /var/log/nginx/mysite_local.error.log;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

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

有人可以告诉我需要做哪些更改来实现我的目标吗?

谢谢。

英文:

I'm attempting to make all path and file requests direct to /index.php if they do not exist.

Currently the root (root /var/www/mysite.local/html/public/) only has one file (index.php) and no sub-directories.

The goal is that unless a path and/or file name exist, no matter what the path(s) or filename are, regardless of extension (.php .jpg .gif .html .whatever), the request should direct to /index.php

Currently this only works for paths (/ or /something/or/other/).

This is what I have for my /etc/nginx/sites-available/mysite.local

server {
            listen 80;
            root /var/www/mysite.local/html/public/;
            index index.php;
            server_name mysite.local;

            access_log /var/log/nginx/mysite_local.access.log;
            error_log /var/log/nginx/mysite_local.error.log;

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

            location ~ \.php$ {
                         include snippets/fastcgi-php.conf;
                         fastcgi_pass unix:/run/php/php8.2-fpm.sock;
                         fastcgi_param SCRIPT_FILENAME $document_root/index.php;
            }

            location ~ /\.ht {
                         deny all;
            }

            location = /favicon.ico {
                         log_not_found off;
                         access_log off;
            }

            location = /robots.txt {
                         allow all;
                         log_not_found off;
                         access_log off;
           }

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

Can someone tell me what changes I need to make to accomplish my goal?

Thank you.

答案1

得分: 1

由于它仅适用于路径,这就是为什么它仅在该位置块上工作的原因。您的 try_files $uri $uri/ /index.php?$args; 应该正好做您想要的事情。问题在于,由于由nginx定义的重要性顺序,nginx将最后检查此位置块,具体请参考:https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms#how-nginx-chooses-which-location-to-use-to-handle-requests

因此,如果您访问任何 .js 文件,您将跳转到最后一个位置块,而没有定义它应该使用 index.php 作为后备。我认为将 try_files $uri $uri/ /index.php?$args; 添加到每个位置块应该可以解决您的问题。尽管它可能无法在php位置块内工作。

英文:

The reason why its only working for paths is, that it's only working for that location block. Your try_files $uri $uri/ /index.php?$args; should be doing exactly what you want. The "proplem" is, that nginx will check this location block last, due to the importance order defined by nginx: https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms#how-nginx-chooses-which-location-to-use-to-handle-requests

So if you access any .js file, you will jump into the last location block and there is nowhere defined it should use the index.php as fallback. I think adding the try_files $uri $uri/ /index.php?$args; to every location block should solve your problem. Tho it might not work inside the php location block.

huangapple
  • 本文由 发表于 2023年7月11日 03:58:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定