Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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;
}
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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;
  }
}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.