Назад | Перейти на главную страницу

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;
  }

}

Заранее благодарим за внимание и ответы.

Поскольку вы обслуживаете 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 ...;
        ...
    }
}

Видеть этот документ для подробностей.