【问题标题】:nginx rewrite to php file - File is being downloadednginx 重写为 php 文件 - 正在下载文件
【发布时间】:2013-03-30 13:10:11
【问题描述】:

首先,请原谅我是 nginx 的新手。

单个站点,在根目录中运行 Wordpress,在子目录中运行各种其他应用程序。据我所知,Wordpress 永久链接/重写运行良好。

问题:所有 php 文件在直接浏览时都能正常工作。但是,当访问 /apply/ 时,文件会被下载和/或在浏览器中显示为纯文本。如果我直接浏览到 /forums/apply.php,它可以正常工作。

此站点的 nginx 配置:

server_name site;
root /var/www/site;
index index.php index.html index.htm;

location /apply {
  rewrite ^/apply/ /forums/apply.php break;
}

location / {
  if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php last;
    break;
  }
}


location ~ \.php {
    # for security reasons the next line is highly encouraged
    try_files $uri /index.php =404;

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

    # if the next line in yours still contains $document_root
    # consider switching to $request_filename provides
    # better support for directives such as alias
    fastcgi_param  SCRIPT_FILENAME    $request_filename;

    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    # If using a unix socket...
    # fastcgi_pass unix:/tmp/php5-fpm.sock;

    # If using a TCP connection...
    fastcgi_pass 127.0.0.1:9000;
}

非常感谢任何和所有建议。

【问题讨论】:

    标签: php nginx rewrite


    【解决方案1】:

    改变

    rewrite ^/apply/ /forums/apply.php break;
    

    rewrite ^/apply/ /forums/apply.php last;
    

    【讨论】:

    • 我遇到了同样的问题,将指令更改为 last 并没有解决它。
    • 您的重写指令是在位置块内还是在服务器块内? breaklast 在服务器块内表示相同的东西。它们仅在位置块内时不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2018-01-23
    • 2014-10-11
    • 1970-01-01
    相关资源
    最近更新 更多