nginx.conf用于url路由

Gui*_*ido 0 php nginx url-routing

我正在尝试让我的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>
Run Code Online (Sandbox Code Playgroud)

小智 8

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

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

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

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

例如,然后/blablabla将屏蔽该URL,但/index.php?param=blablabla只要/blablabla不是目录就拉起来.

希望这可以帮助!