我进退两难,无法弄清楚为什么会发生这种情况。由于某种原因,当我打开我的网站时,index.php 文件正在被执行。我已经能够将其缩小到 NginX/PHP-FPM,方法是将其添加file_put_contents('runs.txt', 'executed'.PHP_EOL, FILE_APPEND);
到 index.php 的顶部并使用网站和命令行执行它。如果我从命令行执行它,它只会输出一个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;
}
php.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;
}
网站配置文件
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。有什么想法吗?
答案1
我找到了问题所在。由于如果指定的 URI 不存在,try_files
就会自动重定向到index.php
,因此缺少一张图片,当它打开时,它会index.php
再次打开。
我能够确定丢失的图像,http://example.com/images/google.png
因此我找到该图像并将其放在正确的文件夹中(而不是重定向回index.php)。
另一个更合适的解决方案是在文件中(块之前)有一个带有语句location /images
的块,如果图像不存在,则发送 404 未找到状态代码。try_files
website.conf
location /
location /images {
try_files $uri =404;
}