1

I am trying to migrate from apache to nginx. The php sites that I am hosting need to access a shared library which turns out to be an alias directory. Below is the configuration I came up with. html files work fine, but php files giving 404. I have read through and tried most (if not all) of the answers to the similar questions with no any success. Any hint on what might be causing the issue in my case?

location /wtlib/ {
    alias /var/www/shared/wtlib_4/;
    index index.php;
}

location ~ /wtlib/.*\.php$ {
    alias /var/www/shared/wtlib_4/;
    try_files $uri =404;
    if ($fastcgi_script_name ~ /wtlib(/.*\.php)$) {
        set $valid_fastcgi_script_name $1;
    }
    fastcgi_pass 127.0.0.1:9013;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/shared/wtlib_4$valid_fastcgi_script_name;
    fastcgi_param REDIRECT_STATUS 200;
    include /etc/nginx/fastcgi_params;
}

Thanks all !

Update: Following seems to be working fine:

location /wtlib/ {
    alias /usr/share/php/wtlib_4/;
    location ~* .*\.php$ {
      try_files $uri @php_wtlib;
    }

    location ~* \.(html|htm|js|css|png|jpg|jpeg|gif|ico|pdf|zip|rar|air)$ {
      expires 7d;
      access_log off;
    }
}

location @php_wtlib {
    if ($fastcgi_script_name ~ /wtlib(/.*\.php)$) {
        set $valid_fastcgi_script_name $1;
    }
    fastcgi_pass $byr_pass;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/share/php/wtlib_4$valid_fastcgi_script_name;
    fastcgi_param REDIRECT_STATUS 200;
    include /etc/nginx/fastcgi_params;
}
code90
  • 173
  • 1
  • 7

1 Answers1

0

Your line try_files $uri =404; is the reason, but you could improve your config:

server {
  # other code ...
  index index.php;
  location /wtlib/ {
    alias /var/www/shared/wtlib_4/;
    location ~* .*\php$ {
      try_files $uri @php;
    }
  }
  location @php {
    fastcgi_pass 127.0.0.1:9013;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param REDIRECT_STATUS 200;
    include /etc/nginx/fastcgi_params;
  }
}
Fleshgrinder
  • 3,798
  • 2
  • 17
  • 20
  • tried with your corrections, and got a "Access denied." Actually, this is the first time I got something other than 404. Can you think of anything ? – code90 Aug 28 '12 at 23:38
  • Maybe the script filename is the problem? Please try the updated configuration. – Fleshgrinder Aug 29 '12 at 16:47