nginx rewrite issues - php

I just switched from apache to nginx, just for testing and I experience the following problem. I am using this config for nginx
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
Now I have a php script makethumbs.php that automatically resize images displayed on my website. With apache, works just fine. With nginx I get this error:
2011/12/29 15:13:17 [error] 15548#0: *9 open() "/usr/share/nginx/html/makethumbs.php/0737438664-22.jpg" failed (20: Not a directory), client: 193.138.192.81, server: www.escortele.eu, request: "GET /makethumbs.php/0737438664-22.jpg?width=48&height=64&image=/members/escorte/0737438664-22.jpg HTTP/1.1", host: "escortele.eu:88", referrer: "http://escortele.eu:88/"
The problem is that it sees makethumbs.php as a directory and it should be a script not a directory.
I can't figure it out what rewrite rule to use, only for makethumbs.php so it acts like a script and not like a directory.

You should have pasted the rest of your configuration file because what you pasted has nothing to do with what you want.
That error is because you have "try_files $uri $uri/ /index.php;" somewhere in your config. You need to remove the $uri/ from that to fix the error you pasted.

The problem is not in
location / {
try_files $uri $uri/ /index.php;
}
That part is good. You need it.
Since you didn't post your entire config I can't be sure what you're problem is, but I can tell you what my problem was in the same type of situation. Following the recomendation of others, I had a location defined for image files as follows:
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
That was the problem. Because Nginx uses the most specific location that matches, the URL ending with .jpg would match this location and this location doesn't tell it to use index.php. I just got rid of this location and it worked.
Additional tips:
define your root in the server block and not in location blocks.
I don't think you don't need "fastcgi_index index.php;"
You don't need to hard-code your document root:
change
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
to
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

This is the script I am using for thumbnails: http://escorte.pro:88/makethumbs.txt
My nginx config is as follows: http://escorte.pro:88/nginx.txt
The error I get is:
2014/08/27 14:59:07 [error] 20986#0: *86 open() "/usr/share/nginx/html/escorte.pro/makethumbs.php/0737835261-79.jpg" failed (20: Not a directory), client: 83.166.220.234, server: escorte.pro, request: "GET /makethumbs.php/0737835261-79.jpg?width=48&height=64&image=/members/escorte/0737835261-79.jpg HTTP/1.1", host: "escorte.pro:88", referrer: "http://escorte.pro:88/"
2014/08/27 14:59:07 [error] 20986#0: *87 open() "/usr/share/nginx/html/escorte.pro/makethumbs.php/0743844296-60.jpg" failed (20: Not a directory), client: 83.166.220.234, server: escorte.pro, request: "GET /makethumbs.php/0743844296-60.jpg?width=48&height=64&image=/members/escorte/0743844296-60.jpg HTTP/1.1", host: "escorte.pro:88", referrer: "http://escorte.pro:88/"
The makethumbs.php script works perfectly fine on apache
Any clues?
I hope this info si more complete than the previous...

Related

Automated nginx proxy with fastcgi backends

I can not figure out, how to connect jwilder/nginx-proxy directly to a fastcgi backend. As i am using docker stack, this is the corresponding compose-file:
php-fpm:
image: some/php-app
working_dir: /var/www/application
environment:
VIRTUAL_HOST: php-fpm.example
VIRTUAL_PROTO: fastcgi
VIRTUAL_PORT: 9000
VIRTUAL_ROOT: /var/www/application/public
This config leads to a 404 error:
2018/10/30 07:58:13 [error] 304: *5357 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.255.0.2, server: php-fpm.example, request: "GET /api/settings HTTP/2.0", upstream: "fastcgi://10.0.0.203:9000", host: "php-fpm.example"
If I understand the situation correctly, something like this is missing:
server {
root /var/www/application/public;
index index.php;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
}
When i place this location file under /etc/nginx/vhost.d/default_location or even under /etc/nginx/vhost.d/{VIRTUAL_HOST}_location, i get a error saying server directive is not allowed here. When i only use the "location block", i get an 502 Bad Gateway Error.
Since i have multiple backends, which are mostly identical, some default config which covers most settings would be great.
Has anyone got something like this working?

Nginx serving static files with 404 status code?

I'm trying to let Nginx serve my authenticated admin users some static files with PHP. This works great, I get back the file. But it's served with a 404 status code..
I'm using the following (Symfony / Silex) php code:
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
$filePath = '/usr/share/nginx/project/src/path/to/my/protected/static/dir/' . $file;
if (empty($file) || !is_readable($filePath)) {
$app->abort(404);
}
$response = new BinaryFileResponse($filePath);
$response->trustXSendfileTypeHeader();
$response->setPrivate();
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
$file,
iconv('UTF-8', 'ASCII//TRANSLIT', $file)
);
$response->headers->addCacheControlDirective('must-revalidate', true);
return $response;
And here's my nginx config:
server {
listen 443;
listen [::]:443;
root /usr/share/nginx/project/web;
index index.php;
error_page 401 403 404 /404.html;
server_name example.com;
rewrite ^/(.*)/$ /$1 permanent;
location / {
# First attempt to serve request as file, then
# as directory, then let php handle the file
try_files $uri $uri/ /index.php$is_args$args;
index index.php;
autoindex off;
location ~* \.(svg|jpg|jpeg|png|gif|ico|css|js)$ {
expires 150d;
}
}
location ~ \.php$ {
set $path_info $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
try_files $uri $uri/ /index.php$is_args$args;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param APP_ENV production;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The protected dir lies outside of the root in the nginx config (/usr/share/nginx/project/web).
I've found these kind of error messages in the logs:
open() "/usr/share/nginx/project/web/admin/static/admin.js" failed
(2: No such file or directory),
request: "GET /admin/static/admin.js HTTP/1.1"
Where /admin/static/admin.js was indeed the requested url.
Update 1
It looks like nginx always tries to open the url and adds an entry to the error log even php handles the response just fine.
Even if I replace all the php code for just: return new Response('test', 200); the response code for the body 'test' is still 404...
I also tried adding an extra location block in my nginx config:
location /protected_admin_files {
internal;
alias /usr/share/nginx/project/src/path/to/my/protected/static/dir;
}
And then try to redirect to the file like this:
return new Response('', 200, [
'X-Accel-Redirect' => '/protected_admin_files/' . $file
]);
But also without luck. Same 404 with the right response body...
I've found the cause myself..
This was what I found in the error log once I set the error level to debug (here's the full log)
...
[debug] rewrite phase: 1
[debug] http script regex: "^/(.*)/$"
[notice] "^/(.*)/$" does not match "/admin/static/admin.js", request: "GET /admin/static/admin.js HTTP/1.1", host: "example.com"
[debug] test location: "/"
[debug] test location: "protected_admin_files"
[debug] test location: ~ "\.(svg|jpg|jpeg|png|gif|ico|css|js)$"
[debug] using configuration "\.(svg|jpg|jpeg|png|gif|ico|css|js)$"
...
[debug] http filename: "/usr/share/nginx/project/web/admin/static/admin.js"
[debug] add cleanup: 00000000025AE108
[error] open() "/usr/share/nginx/project/web/admin/static/admin.js" failed (2: No such file or directory), client: 24.132.134.203, server: example.com, request: "GET /admin/static/admin.js HTTP/1.1", host: "example.com"
[debug] http finalize request: 404, "/admin/static/admin.js?" a:1, c:1
[debug] http special response: 404, "/admin/static/admin.js?"
[debug] internal redirect: "/index.php?"
...
Apparently, the nested location in my first first location / block was causing the 404.
location / {
try_files $uri $uri/ /index.php$is_args$args;
index index.php;
autoindex off;
location ~* \.(svg|jpg|jpeg|png|gif|ico|css|js)$ {
expires 150d;
}
}
It got matched because of the extension, making Nginx go looking for the file. Since it's then not found, the 404 is set and apparently not overwritten later in the process when php returns the X-Accel-Redirect header :(.

nginx+php-fpm issue not able to call other php files in folder

I have a nginx and php-fpm config but when i access it from browser, only index.php is getting executed but rest of the files i am not able to call .
nginx config
{
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
keepalive_timeout 15;
keepalive_requests 2048;
server_tokens off;
upstream php
{
server unix:/tmp/php-cgi.socket;
server serverip:9000;
}
access_log /var/log/nginx/access.log main;
include /etc/nginx/conf.d/*.conf;
}
config in /etc/nginx/conf.d/
server {
root /var/www/Cachet/public/;
location / {
try_files $uri $uri/ /index.php index.php;
}
server_name serverip ; # Or whatever you want to use
listen 80 default;
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_keep_conn on;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
These are few lines from error.log and access.log
2015/11/06 12:40:53 [error] 19346#0: *1 FastCGI sent in stderr:
"Unable to open primary script: /var/www/Cachet/public/dashboard.php
(No such file or directory)" while reading response header from
upstream, client: Client IP, server: Server IP, request: "GET
/dashboard.php HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php5-fpm.sock:", host: "Server IP"
2015/11/06 12:41:05 [error] 19346#0: *1 FastCGI sent in stderr:
"Unable to open primary script: /var/www/Cachet/public/autoload.php
(No such file or directory)" while reading response header from
upstream, client: Client IP, server: Server IP, request: "GET
/autoload.php HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php5-fpm.sock:", host: "Server IP"
since there was no response here then with help from my colleague i was able to find two problem here in config file because of which i was not able to call multiple php files in separate folder ..
try_files $uri $uri/ /index.php index.php;
instead it needed
try_files $uri $uri/ /index.php$is_args$args;
Alos since it was not loading the images the line which was missing was
include /etc/nginx/mime.types; in location block of conf.d/default.conf.
Check if your installed PHP version and PHP version inside config.d do not match each other. If that is the case, change PHP version inside conf.d file to your installed PHP version. Reload nginx.
fastcgi_pass unix:/var/run/php5-fpm.sock;

Setting up laravel application with nginx

I'm trying to install a fresh installation of Laravel on my nginx setup.
I get presented with a blank page, and my error log states the following:
PHP message: PHP Fatal error: require(): Failed opening required '/srv/laravel/public/../bootstrap/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /srv/laravel/public/index.php on line 21" while reading response header from upstream, client: **.***.***.**, server: laravel.{domain}.nl, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fcgi-laravel-php-fcgi-0.sock:", host: "laravel.{domain}.nl"
I have checked if the file exists. From the cli it actually returns 1 if i use
php -r "echo file_exists(__DIR__.'../bootstrap/autoload.php');"
i have also checked if my PHP version is up to date. The version number is 5.4.4
I have also ran composer update in the application root folder, but to no avail.
I assume this has nothing to do with my nginx setup, as it does load the index.php.
I don't have too much experience with nginx though so i might be wrong.
UPDATE: I'll post the nginx config here
server {
listen *:80;
server_name laravel.{domain}.nl;
access_log /var/log/nginx/laravel.access.log;
error_log /var/log/nginx/laravel.error.log;
root /srv/laravel/public;
index index.html index.htm index.php;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location / {
index index.html index.htm index.php; #try static .html file first
##try_files $uri $uri/ /index.php; <<Wrong!! this will break bundles like OneAuth for example
try_files $uri $uri/ /index.php?q=$uri&$args;
}
# catch all
error_page 404 /index.php;
location ~ [^/]\.php(/|$) {
fastcgi_index index.php;
include fcgi.conf;
fastcgi_pass unix:/var/run/php-fcgi-laravel-php-fcgi-0.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I'm at a loss at this moment... Can anyone help me?
I'll provide more information if neccesary. Excuse me for any bad English, thank you.
1 - How did you installed Laravel?
2 - Have you tried to dump the autoload files again? If not:
composer dump-autoload
Source: Laravel docs
I found out that the open_basedir was not set so i couldn't get access to that folder. Setting the open_basedir to %app%/public got it working for me.
You can alternative disable open_basedir for that specific domain ,follow these steps :
In ispconfig3 tabs ,click on sites tab
2.Click on the laravel based site's domain
3.Now click on the options tab
3.put "none" in the PHP open_basedir field
none
4.then in the nginx Directives , put this for urls to work :
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}

unable to implement php with nginx

I have nginx working with arch linux. I am having trouble adding php and fastcgi. I installed the php and php-fpm packages. php-fpm service is on. When I access a file with a php extension, the browser displays the nginx error page and I get the following nginx error log message added:
2013/01/02 22:39:43 [error] 721#0: *27 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.13, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.1.19"
the contents of the php file seem to make no difference. I have been unable to locate php error log or create my own. I added the following to /etc/php/php.ini:
error_log = /var/log/php_error.log
and I made the file world writable but nothing goes there. I added the following to /etc/nginx/nginx.conf:
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi.conf;
}
uncommenting the commented lines changes nothing. Commenting the uncommented lines changes the browser page displayed from error page to file not found page.
Any help would be greatly appreciated. Thanks in advance.
Try changing your configuration file to this:
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
I figured it out. I had to uncomment a line in the php-fpm.conf file to have fastcgi listen on the right port.

Categories