我处于困境和艰难的境地之间,试图弄清楚为什么会这样。出于某种原因,当我打开我的网站时,正在执行 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。有任何想法吗?
我发现了问题。因为如果指定的 URI 不存在
try_files
会自动重定向到,因此缺少一个图像,当它打开时,它会再次打开。index.php
index.php
我能够确定丢失的图像,
http://example.com/images/google.png
所以我找到了该图像并将其放置在正确的文件夹中(而不是被重定向回 index.php)。另一个更合适的解决方案是在文件中(在块之前)有一个
location /images
带有try_files
语句的块,如果图像不存在,则发送 404 not found 状态代码。website.conf
location /