nginx 下载 php 而不是处理它

nginx 下载 php 而不是处理它

我一直在尝试在 nginx 中创建一个简单的别名,而我能做的就是让服务器将文件发送给我。

当我在浏览器中访问 /xhprof_html 时,服务器要求我下载 index.php 文件而不是执行它。我做错了什么?

以下是当前网站的完整 nginx 配置:-

log_format timed_combined '$remote_addr - $remote_user [$time_local]  '
    '"$request" $status $body_bytes_sent '
    '"$http_referer" "$http_user_agent" '
    '$request_time $upstream_response_time $pipe';

server {
  location ~ ^/xhprof_html/(.*)$ {
    alias /usr/share/php/xhprof_html/$1;
  }

  large_client_header_buffers 8 32k;
  listen 8080;
  index index.html index.htm index.php;
  server_name  test.k.dk *.test.k.dk ubuntu-14 localhost;
  access_log  /var/log/nginx/test.k.dk.access.log timed_combined;
  error_log /var/log/nginx/test.k.dk.error.log debug;
  rewrite_log on;
  root   /srv/www/kdrupal/current;
#  root /usr/share/php/xhprof_html;
  real_ip_header      X-Forwarded-For; #Put the Header that your varnish/proxy set

#  location /xhprof_html {
#   autoindex on;
#        alias /usr/share/php/xhprof_html/;
#  }

  # Block all svn access
  if ($request_uri ~* ^.*\.svn.*$) {
     return 404;
  }

  # Block all git access
  if ($request_uri ~* ^.*\.git.*$) {
     return 404;
  }

  location = /favicon.ico {
          log_not_found off;
          access_log off;
  }

  location = /robots.txt {
          allow all;
          log_not_found off;
          access_log off;
  }

  # This matters if you use drush
  location = /backup {
          deny all;
  }

  # Very rarely should these ever be accessed outside of your lan
  location ~* \.(txt|log)$ {
          allow 192.168.0.0/16;
          deny all;
  }

#  location ~ \..*/.*\.php$ {
#          return 403;
#  }

  location / {
    index  index.html index.htm index.php;
    try_files $uri @rewrite;
  }

  location @rewrite {
          # Some modules enforce no slash (/) at the end of the URL
          # Else this rewrite block wouldn't be needed (GlobalRedirect)
          rewrite ^/(.*)$ /index.php?q=$1;
  }

#  location ~ \.php$ {
#    set $socket /var/run/php-fpm-www.sock;
#    if ($request_uri ~* /status.php) {
#        set $socket /var/run/php-fpm-status.sock;
#    }
#    fastcgi_split_path_info ^(.+\.php)(/.+)$;
#    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#    include fastcgi_params;
#    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#    fastcgi_intercept_errors on;
#    fastcgi_pass unix:$socket;
#  }

  location ~ ^/order/callback/ {
      try_files $uri @rewrite;
  }

  location ~ ^/infosoft/customernumberchanged/ {
      try_files $uri @rewrite;
  }

  location ^~ /cdn/farfuture/ {
    auth_basic  "off";
    tcp_nodelay   off;
    access_log    off;
    log_not_found off;
    gzip_http_version 1.0;
    if_modified_since exact;
    location ~* ^/cdn/farfuture/.+\.(?:css|js|jpe?g|gif|png|ico|bmp|svg|swf|pdf|docx?|xlsx?|pptx?|tiff?|txt|rtf|class|otf|ttf|woff|eot|less|mp3)$ {
      expires max;
      add_header X-Header "CDN Far Future Generator 1.0";
      add_header Cache-Control "no-transform, public";
      add_header Last-Modified "Wed, 20 Jan 1988 04:20:42 GMT";
      rewrite ^/cdn/farfuture/[^/]+/[^/]+/(.+)$ /$1 break;
      try_files $uri @rewrite;
    }
    location ~* ^/cdn/farfuture/ {
      expires epoch;
      add_header X-Header "CDN Far Future Generator 1.1";
      add_header Cache-Control "private, must-revalidate, proxy-revalidate";
      rewrite ^/cdn/farfuture/[^/]+/[^/]+/(.+)$ /$1 break;
      try_files $uri @rewrite;
    }
    try_files $uri @rewrite;
  }

  location ~* \.(js|css|png|jpg|jpeg|gif|ico|ttf|woff|mp3)$ {
          expires max;
          log_not_found off;
          try_files $uri @rewrite;
  }

  location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    allow 10.88.130.41;
    allow 80.254.154.43;
    allow 193.238.186.146;
    deny all;
  }

  location ~ ^/(status|ping)$ {
    access_log off;
    allow 127.0.0.1;
    allow 10.88.130.41;
    allow 80.254.154.43;
    allow 193.238.186.146;
    deny all;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php-fpm-www.sock;
  }

}

提前感谢您的关注和回答。

答案1

由于您从两个不同的根目录提供 PHP,因此您需要两个locations来提供 PHP。目前,您已创建了一个仅提供普通文件的位置。

alias此外,这里不需要使用,因为本地路径以 URI 结尾。root在这种情况下使用起来要简单得多。

使用^~带前缀的修饰符location会强制它优先于同一级别的正则表达式位置。

我建议您使用嵌套位置块从其他文档根目录为您的 PHP 提供服务:

location ^~ /xhprof_html/ {
    root /usr/share/php;

    location ~ \.php$ {
        try_files $uri =404;

        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass ...;
        ...
    }
}

这个文件了解详情。

相关内容