Nginx

nginx - cURL 檢索 PHP 文件,但通過 Web 瀏覽器訪問會提供原始碼

  • May 15, 2017

我在使用 nginx 時遇到了一些問題。我在 Raspberry Pi B+ (Raspbian Jessie) 和 PHP 上使用 FastCGI 設置了 nginx。當我嘗試使用 cURL 檢索頁面時,它返回 PHP 生成的 HTML。

為 fastcgi 請求提供服務的 nginx 伺服器塊

server {
   listen 80 default_server;
   listen [::]:80 ipv6only=on default_server;

   # SSL configuration
   #
   # listen 443 ssl default_server;
   # listen [::]:443 ssl default_server;
   #
   # Self signed certs generated by the ssl-cert package
   # Don't use them in a production server!
   #
   # include snippets/snakeoil.conf;

   root /usr/share/nginx/html;

   # Add index.php to the list if you are using PHP
   index index.php index.html index.htm index.nginx-debian.html;

   server_name _;

   location / {
           # First attempt to serve request as file, then
           # as directory, then fall back to displaying a 404.
           try_files $uri $uri/ =404;
   }

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
   location ~ \.php$ {
   #       include snippets/fastcgi-php.conf;
           include fastcgi_params;
           try_files $uri =404;
           fastcgi_split_path_info ^(.+\.php)(/.+)$;

   #
   #       # With php5-cgi alone:
   #       fastcgi_pass 127.0.0.1:9000;
           # With php5-fpm:
           fastcgi_pass unix:/var/run/php5-fpm.sock;
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

   }


   # deny access to .htaccess files, if Apache's document root
   #oncurs with nginx's one
   #
   #location ~ /\.ht {
   #       deny all;
   #}
}

PHP文件

<?php
echo "PHP Test";
?>

瀏覽器響應:下載它(chrome)或將其顯示為原始碼(其他瀏覽器)

捲曲:

  • 在 DNS 記憶體中找不到主機名
  • 正在嘗試 ::1…
  • 連接到 localhost (::1) 埠 80 (#0)

GET /test_2.php HTTP/1.1 使用者代理:curl/7.38.0 主機:localhost 接受:/

< HTTP/1.1 200 正常

  • 伺服器 nginx 未列入黑名單 < 伺服器:nginx < 日期:Fri, 12 May 2017 04:21:20 GMT < Content-Type: text/html; charset=UTF-8 < Transfer-Encoding: chunked < Connection: keep-alive < PHP Test
  • 到主機 localhost 的連接 #0 保持不變

nginx access.log

::1 - - [12/May/2017:12:21:20 +0800] "GET /test_2.php HTTP/1.1" 20019 "-" "curl/7.38.0"
192.168.0.132 - - [12/May/2017:12:26:28 +0800] "GET /test_2.php HTTP/1.1" 200 28 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"

nginx 錯誤日誌

2017/05/12 11:50:41 [error] 21715#0: *64 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.0.132, server: ~., request: "GET /favicon.ico HTTP/1.1", host: "192.168.0.113"
2017/05/12 11:53:02 [error] 21715#0: *66 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.0.132, server: ~., request: "GET /favicon.ico HTTP/1.1", host: "192.168.0.113"
2017/05/12 11:54:18 [notice] 21737#0: signal process started
2017/05/12 11:54:22 [notice] 21747#0: signal process started

編輯:在蒂姆下面的評論之後,我執行 curl 強制 IPv4,它確實返回了原始碼。

我想我知道為什麼會這樣。我使用 RegEx 解析所有名稱的第二個伺服器塊~.設置為偵聽 IPv4 埠 80,並且它沒有 CGI 處理程序。但是,預設伺服器塊設置為在埠 80 上偵聽 IPv6。由於 nginx 僅在測試其他伺服器後才回退到預設伺服器,因此代理伺服器塊無法處理 PHP,因此它將其作為原始碼返回。在 IPv6 上,nginx 無法使用代理伺服器塊來處理請求,因此它將它傳遞給執行 FastCGI 伺服器的預設伺服器,因此它可以返回phpinfo().

解決方案

我應該將 location /git/代理通行證塊放在預設伺服器塊中。

引用自:https://serverfault.com/questions/849649