0

我试图让我的 index.php 来处理 http 路由,所以让我的应用程序变得安静。

我在 nginx.cong 中使用了 try_files 指令,但没有用,我点击 /blablabla 并没有通过 index.php 而是抛出 404。这是我当前的 nginx.conf

<pre>

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
server {
 location /  {
   try_files $uri $uri/ /index.php;
}

}
   
}

</pre>
4

2 回答 2

9

你可能想尝试这样的事情,对我来说就像一个魅力:

location / { 
    try_files $uri $uri/ @rules; 
} 

location @rules { 
    rewrite ^/(.*)$ /index.php?param=$1; 
}

这将查找/您的 Web 根目录的位置。您可以在此目录中找到所有可通过 Web 访问的文件。如果文件存在,它会将您带到该文件。如果没有,那么它会将您扔到 @rules 块中。您可以使用正则表达式匹配来改变您的 url 格式。但简而言之,(.*)匹配您 url 中的任何字符串并将您带到您的索引。我稍微修改了您编写的内容,以将原始输入作为参数提供给 index.php。如果您不这样做,您的脚本将没有任何有关如何路由请求的信息。

例如,然后 go to将屏蔽 url,但只要不是目录/blablabla就会拉起。/index.php?param=blablabla/blablabla

希望这可以帮助!

于 2013-09-11T08:07:24.847 回答
1
server {
    listen 80;
    server_name example.com;
    index index.php;
    error_log /path/to/example.error.log;
    access_log /path/to/example.access.log;
    root /path/to/public;

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

    location ~ \.php {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
    }
}
于 2019-01-27T08:28:15.397 回答