0

I have been trying to make a simple alias in nginx and the furtherest I could get is to get the server to send me the file.

When I visit /xhprof_html in my browser the server asks me to download the index.php file instead of executing it. What am I doing wrong?

Here is my full nginx configuration for the current site:-

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

}

Thanks in advance for your attention and answers.

dasj19
  • 433
  • 1
  • 3
  • 11

1 Answers1

0

Because you are serving PHP from two different roots, you will need two locations to serve PHP. At the moment, you have created a location to serve normal files only.

Also, the use of alias is not required here, as the local path ends with the URI. It is much simpler to use root in this case.

Using the ^~ modifier with a prefix location forces it to take precedence over regex locations at the same level.

I would suggest that you use nested location blocks to serve your PHP from this other document root:

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

See this document for details.

Richard Smith
  • 12,834
  • 2
  • 21
  • 29