简体   繁体   English

Nginx-重写后找不到php文件

[英]Nginx - php-files not found after rewrite

I'm trying to rewriting /assets/* -> to /theme/theme_1/* . 我正在尝试将/ assets / *->重写为/ theme / theme_1 / * The rewrite url works with all files except .php-files. 重写URL适用于.php文件以外的所有文件。

Example file structure: 示例文件结构:

  • /theme/theme_1/images/image.jpg /theme/theme_1/images/image.jpg
  • /theme/theme_1/images/user.jpg /theme/theme_1/images/user.jpg
  • /theme/theme_1/ajax/register.php /theme/theme_1/ajax/register.php
  • /theme/theme_1/ajax/read.php /theme/theme_1/ajax/read.php

The problem is the PHP-files, I get a 404 with this url: wget http://example.com/assets/ajax/read.php . 问题是PHP文件,我使用以下URL获得404:wget http://example.com/assets/ajax/read.php File is found (200) using full path http://example.com/theme/theme_1/ajax/read.php 使用完整路径http://example.com/theme/theme_1/ajax/read.php找到(200)文件

All other file works fine (200): wget http://example.com/assets/images/image.jpg 所有其他文件都可以正常工作(200):wget http://example.com/assets/images/image.jpg

nginx config: Nginx的配置:

server {
  listen 80 default_server;

  root /var/www/html;
  index index.php index.html
  server_name mysite.com;

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
  }

  location /assets {
    rewrite ^/assets/(.*) /theme/theme_1/$1 break;
  }

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

Okie you should try this 好的,你应该试试这个

  location /assets/ {
    alias /var/www/html/theme/theme_1/;
  }

If that doesn't work then try 如果这不起作用,请尝试

  location /assets/ {
    alias /var/www/html/theme/theme_1/;
    try_files $uri $uri/ /index.php?$args;
  }

Edit-1 编辑1

On second look I realize the previous answer won't work as ~ \\.php { block will catch everything with php extension and the other assets block can never get called. 从第二个角度看,我意识到前面的答案将不起作用,因为~ \\.php {块将捕获具有php扩展名的所有内容,而其他assets块将永远无法调用。 So the solution is to nest the rewrite inside the php block. 因此解决方案是将重写内容嵌套在php块中。 So use 所以用

  location ~ \.php$ {
    rewrite ^/assets/(.*)$ /theme/theme_1/$1;

    include snippets/fastcgi-php.conf;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM