NginX 導致 PHP 文件被執行兩次
我處於困境和艱難的境地之間,試圖弄清楚為什麼會這樣。出於某種原因,當我打開我的網站時,正在執行 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 /
location /images { try_files $uri =404; }