AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 571343
Accepted
ub3rst4r
ub3rst4r
Asked: 2014-02-01 09:23:20 +0800 CST2014-02-01 09:23:20 +0800 CST 2014-02-01 09:23:20 +0800 CST

NginX 导致 PHP 文件被执行两次

  • 772

我处于困境和艰难的境地之间,试图弄清楚为什么会这样。出于某种原因,当我打开我的网站时,正在执行 index.php 文件。file_put_contents('runs.txt', 'executed'.PHP_EOL, FILE_APPEND);通过添加到 index.php 的顶部并使用网站和命令行执行它,我已经能够将其缩小到 NginX/PHP-FPM 。如果我从命令行执行它,它只输出一个executed,而如果我从网站(通过 NginX)执行它,它输出两个executed. 该脚本也不会重定向到自身,因为它只是返回 200 响应代码。除了必须通过执行两次脚本来使用更多内存之外,它还会通过第二次生成 CSRF 来破坏 CSRF 保护,因此 CSRF 是无用的。

下面是我的 nginx 配置文件:

nginx.conf

user  nginx;
worker_processes  2;

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


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    # Upstream to abstract backend connection(s) for PHP.
    upstream php {
        #this should match value of "listen" directive in php-fpm pool
        server unix:/var/lib/php-fpm.sock;
    }

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

配置文件

# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ \.php$ {
        #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        include fastcgi_params;

        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 600s;
#       fastcgi_intercept_errors on;
        fastcgi_pass php;
}

网站.conf

server {
    listen       80;
    server_name  xyz.com www.xyz.com;
    root         /usr/share/nginx/xyz.com;
    #access_log  /usr/share/nginx/logs/xyz.com-access_log;
    error_log    /usr/share/nginx/logs/xyz.com-error_log;

    index index.php;

    location / {
       index index.php;

       try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    include php.conf;
}

fastcgi_params

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;
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  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

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;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

如果我更改try_files $uri $uri/ /index.php?q=$uri&$args;为try_files $uri $uri/;并调出,http://www.xyz.com/index.php那么它只会执行一次,但我需要/index.php?q=$uri&$args在那里。我正在使用 nginx 1.4.4 和 PHP 5.4.23 运行 CentOS 6.5。有任何想法吗?

php
  • 1 1 个回答
  • 2367 Views

1 个回答

  • Voted
  1. Best Answer
    ub3rst4r
    2014-02-01T14:54:55+08:002014-02-01T14:54:55+08:00

    我发现了问题。因为如果指定的 URI 不存在try_files会自动重定向到,因此缺少一个图像,当它打开时,它会再次打开。index.phpindex.php

    我能够确定丢失的图像,http://example.com/images/google.png所以我找到了该图像并将其放置在正确的文件夹中(而不是被重定向回 index.php)。

    另一个更合适的解决方案是在文件中(在块之前)有一个location /images带有try_files语句的块,如果图像不存在,则发送 404 not found 状态代码。website.conflocation /

    location /images {
        try_files $uri =404;
    }
    
    • 4

相关问题

  • 用户特定的 Php.ini 当 php 作为模块运行时?

  • 使 php mail() 函数在 ubuntu-server 上工作的步骤是什么?

  • Web 服务器和数据库服务器位于完全不同的位置

  • PHP 作为 CGI 还是 Apache 模块?

  • 通过 VPN 连接什么是远程服务器 IP?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve