nginx + php-fpm - 502 Bad Gateway when rewrite with dash - nginx

I use nginx with php-fpm for my webserver and I have a little trouble with the rewrite system.
So when I want to access to page like /dashboard or /api/info it works correctly. But when I want to access to page with dash in URL like /dashboard/global-infos, I get an error 502 (Bad Gateway).
My configuration for rewriting is:
rewrite ^/index\.php/?(.*)$ /$1 permanent;
location / {
index index.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /index.php/$1 last;
}
rewrite_log on;
When I read log, I do not have log on rewriting, only :
recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 176.190.XXX.XXX, server: domain.com, request: "GET /dashboard/global-infos
Hope you can help me, have a good day !

Related

Try_files directive keeps looking for files in /usr/local/nginx/html

I can't get my head round why my nginx config keeps looking for files in the wrong directory.
I'm trying to map the /files location to /home/user/toto. For that I'm using a regex capture group to remove the /files prefix and search for the files in the directory specified in the root directive.
server {
listen 80;
server_name localhost;
location ~^\/files\/(.*)$ {
root /home/user/toto;
try_files /$1/ /$1 ;
autoindex off;
}
}
The problem is I only get 404s. And that's pretty normal because here is what we can find in error.log :
2022/07/17 11:27:49 [error] 40358#0: *40 open() "/usr/local/nginx/html/test.txt" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /files/test.txt HTTP/1.1", host: "localhost"
As we can see the root directive seems to be completely ignored.
I tried an alternative configuration such as :
server {
listen 80;
server_name localhost;
location ~^\/files\/(.*)$ {
# root /home/user/toto;
try_files /home/user/toto/$1/ /home/user/toto/$1 ;
autoindex off;
}
}
But the problem is still the same. The only change is that the path now appears in the logs as a prefix for the file name :
2022/07/17 11:12:02 [error] 40288#0: *36 open() "/usr/local/nginx/html/home/user/toto/test.txt" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /files/test.txt HTTP/1.1", host: "localhost"
What am I missing ?
Your missing the last parameter from the try_files directive, so that the /$1 is being interpreted as an internal redirect instead of a file term.
Use:
try_files /$1/ /$1 =404;
See this document for details.

Unexpected nginx behavior

I have the following block in my nginx config:
location ~ /$ {
index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
}
location /cat {
try_files /index.php?url=$uri =404;
}
What I expect this to do: Any request sent to http://www.example.com/cat<whatever> will be sent to index.php with the GET variable url set to the request uri. What happens instead based on the rewrite log is that the location /cat block is never hit at all. Here is a relevant excerpt from the rewrite log when I request http://www.example.com/cart/testing/:
2017/03/21 00:32:49 [error] 25711#0: *20566477 "/var/www/vhosts/example.com/httpdocs/cat/testing/index.html" is not found (2: No such file or directory), client: <ip redacted>, server: example.com, request: "GET /cat/testing/ HTTP/1.1", host: "www.example.com"
There are no other entries related to the request.
It was my understanding that nginx processes any prefix location blocks (i.e. location /cat) BEFORE any location blocks involving regex (i.e. location ~ /$). So I'm stumped by this behavior.
Your understanding is incorrect. See this document for details.
Also, your try_files directive is incorrect. Either /index.php?url=$uri or =404 should be the last element in the statement. See this document for details.
You probably want:
index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
location /cat {
try_files $uri $uri/ /index.php?url=$uri;
}
Not sure of the purpose of the other location block, as the index directive is already going to process any URI with a trailing /. See this document for details.

Nginx. Rewrite to static file. Not trivial

actually my first question on stackoverflow, because case is not so easy.
I want nginx to rewrite all requests to static file appending requested url, and i achieved this easily with this rule
location / {
index index.html;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /index.html#!$1 break;
}
BUT this is resulting in 404 and in log i see exactly how this should work, but it isn't.
2016/11/21 11:53:33 [error] 5220#6856: *1 "C:/Projects/ProCongress/web/index.html#!/index.html" is not found (3: The system cannot find the path specified), client: 127.0.0.1, server: congress, request: "GET / HTTP/1.1", host: "congress"
web/index.html#!/index.html - this part is acutally absolultley right, and should work correctly, but isn't...

Setting proper path in Nginx for Symfony2

I need to have my symfony app installed on the same domain as other webapps so I wanted it to sit in /dev/symfony_app path
I tried to use NginX Friendly PHP Framework but solutions from there do not work.
I have such nginx config and it does not work at all too. there is some problem with paths, nor root neither alias directive work for me.
location /dev/symfony_app/ {
root /home/.../public_html/web;
}
location ~ ^/dev/symfony_app/(app|app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param HTTPS off;
}
nginx error log:
request http://domain.com/dev/symfony_app/
2013/06/23 11:25:31 [error] 22549#0: *668
"/home/.../public_html/web/dev/symfony_app/index.php"
is not found (2: No such file or directory), client: *,
server: domain.com, request: "GET /dev/symfony_app/ HTTP/1.1", host: "domain.com"
request https://domain.com/dev/symfony_app
2013/06/23 11:25:37 [error] 22549#0: *668
FastCGI sent in stderr: "Primary script unknown" while
reading response header from upstream, client: *, server: domain.com,
request: "GET /dev/symfony_app HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php-fpm.sock:", host: "domain.com"
request https://domain.com/dev/symfony_app/app_dev.php
2013/06/23 11:27:06 [error] 22549#0: *797
FastCGI sent in stderr: "Primary script unknown" while
reading response header from upstream, client: *, server: domain.com,
request: "GET /dev/symfony_app/app_dev.php HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php-fpm.sock:", host: "domain.com"
Well, what are the dots doing there in your path? You can’t have a directory with three dots as name (at least this would be new to me). The error message from nginx is very specific in that regard. That path doesn’t exist.
server {
listen 80;
server_name _;
root /home/public_html/web;
location / {
location ~* ^/dev/symfony_app/(app|app_dev|config)\.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php-fpm.sock;
}
}
}
That should do the trick.
The absolute local path to the index file of your Symfony installation has to be /home/public_html/web/dev/symfony_app/index.php. A request to http://example.com/dev/symfony_app will map the above location.
I hope this helps, otherwise please leave a comment and describe what else is going wrong.

nginx location fix: Redirect to index.html

I'm runnning nginx v 1.0.4 and we're trying to do the following:
location ~ ^/$ {
rewrite  ^.*$  /index.html  last;
}
Basically: If the user gets to the the default domain http://www.foo.com or http://www.foo.com/ redirect them to http://www.foo.com/index.html
When I add this to my conf file, I get the following:
Starting nginx: nginx: [emerg] unknown directive " " in /etc/nginx/myconf.conf
Thanks in advance.
You can just use rewrite function without location
rewrite ^/$ /index.html last;
or for permanent redirect
rewrite ^/$ /index.html permanent;
to rewrite with parameters, e.g. http://www.foo.com/?param=value -> http://www.foo.com/index.html?param=value
rewrite ^/(\?.*)?$ /index.html$1 permanent;

Resources