Yii2 url重写后缀(php)问题

I Have some url pattern that should work on my website.

[
'pattern'=>'page/result',
'route'=>'site/index',
'suffix'=>'.html'
],
[
'pattern'=>'page/result',
'route'=>'site/index',
'suffix'=>'.php'
],

In these two url suffix .html is working fine but .php suffix is not working in MY Nginx Server. Also check the my site nginx setting.

server {


listen 8081 default_server;
    listen [::]:8081 default_server;

    root /var/www/html/yii2project/frontend/web;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html index.php;


    access_log  /var/log/yii2/access.log;
    error_log   /var/log/yii2/error.log;

    server_name yii2.local;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #try_files $uri $uri/ =404;
           root   /var/www/html/yii2/frontend/web;
           index  index.html index.htm index.php;
           try_files $uri $uri/ /index.php?$args;#now
           #rewrite ^/(.*)$ /$1 last;
           #if ($http_host ~* "^yii2.local:8081"){
           #rewrite ^(.*)$ http://www.yii2.local:8081$1 redirect;
            #}

       }

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


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #
    #   # With php5-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;               # include fastcgi.conf;
                include fastcgi.conf;#now 
        fastcgi_read_timeout 300;    

    } 
}

http://yii2.local:8081/page/result.html (working) http://yii2.local:8081/page/result.php (not working)

Please see the nginx setting and tell me where i'm wrong configurartion.

1个回答



你有三个不同的文档根似乎有点奇怪:</ p>

  / var  / WWW / HTML / yii2project /前端/网络\ N /无功/网络/ HTML / yii2 /前端/网络\ N / USR /共享/ nginx的/ HTML 
</代码> </ PRE>

第一个将用于查找资源文件( .css </ code>和 .js </ code>)。 第二个将用于定位 .html </ code>文件。 第三个将用于定位 .php </ code>文件。</ p>

假设您的所有文件共享一个公共根目录,通常会放置一个 root server </ code>块中的</ code>指令,允许所有 location </ code>块继承该值。</ p>

您应该更改值 您的第一个 root </ code>指令。 删除 location / </ code>块中的 root </ code>指令,并更改 SCRIPT_FILENAME </ code>的定义。</ p>

 <  code> server {
...
root / var / www / html / yii2 / frontend / web;
...
location / {...}
location〜* \。(jpg | jpeg | png | gif | ico | css | js)$ {...}
location~ \ .php $ {
...
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
}
}
</ </ 代码> </ pre>
</ div>

展开原文

原文

You have three different document roots which seems a little strange:

/var/www/html/yii2project/frontend/web
/var/www/html/yii2/frontend/web
/usr/share/nginx/html

The first will be used to locate resource files (.css and .js). The second will be used to locate .html files. The third will be used to locate .php files.

Assuming that all of your files share a common root, it is usual to place a root directive in the server block and allow all location blocks to inherit the value.

You should probably change the value of your first root directive. Delete the root directive in the location / block, and change the definition of SCRIPT_FILENAME.

server {
    ...
    root /var/www/html/yii2/frontend/web;
    ...
    location / { ... }
    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ { ... }
    location ~ \.php$ {
        ...
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

dora1989
dora1989 错误的我写/ var / www / html / yii2project / frontend / web我的根路径是这个/ var / www / html / yii2 / frontend / web我在8081端口上使用apache和nginx所以在nginx站点中可用 默认文件我只在nginx服务器中设置一个项目。 也改变fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; 它不起作用。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐