Nginx rewrite not working at all - mod-rewrite

I am trying to set up a Piwigo to run on Nginx but I got into some issues with links that should contain and index.php like this:
foto.domain.net/index.php/category/3
It seems like a simple task, rewriting /index/category/3 to /index.php/category/3, but no matter what I write in those rewrites, I end up with a 404 error and a never changing error log entry that looks like this:
open() "/srv/http/foto/index/category/3" failed (2: No such file or directory), client: 94.242.246.23, server: ~^(?<prefix>www)?\.?(?<subdomain>[a-z]+)?\.domain\.net$, request: "GET /index/category/3 HTTP/1.1", host: "foto.domain.net", referrer: "http://foto.domain.net/"
The error log looks the same even if I add junk to the rewrite line such as:
rewrite ^/index((/|$).*)$ /JUNK_index.php$1 last;
So the rewrite part is faulty, somehow but I can't figure it out...
server {
listen 80;
server_name domain.net;
return 301 $scheme://www.domain.net$request_uri;
}
server {
listen 80;
server_name "~^(?<prefix>www)?\.?(?<subdomain>[a-z]+)?\.domain\.net$";
root /srv/http/$subdomain;
location #rewrites {
rewrite ^/picture((/|$).*)$ /picture.php$1 last;
rewrite ^/index((/|$).*)$ /index.php$1 last;
# The following is needed for batch operations which use i.php
rewrite ^/i((/|$).*)$ /i.php$1 last;
}
location /$subdomain {
index index.php index.html;
try_files $uri $uri.php $uri/ #rewrites;
}
location ~ ^(?<script_name>.+?\.php)(?<path_info>/.*)?$ {
try_files $script_name = 404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

In general terms, as a checklist:
make sure the target/landing URI exists
this should have output "rewrite": nginx 2>&1 | grep -i -o rewrite
check your error and access log

Related

Laravel Route in NGINX on Windows machine didn't work well

I just install my nginx server on my windows laptop. Then I setup the nginx.conf file like this :
server {
listen 80;
server_name laravelninja.local;
root C:/blablabla/public;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
running the php-cgi using this syntax php-cgi.exe -b 127.0.0.1:9000
and the hosts also
127.0.0.1 laravelninja.local
It's running well at laravelninja.local/, but when I go to other route like laravelninja.local/pizzas, this error came out from nginx
2020/12/23 21:26:52 [error] 8980#11972: *7 CreateFile() "C:/blablabla/public/pizzas" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: laravelninja.local, request: "GET /pizzas HTTP/1.1", host: "laravelninja.local"
and the browser goes to google and search the laravelninja.local/pizzas
this is the code in my route :
Route::get('/', function () {
return view('welcome');
});
Route::get('/pizzas', function () {
return view('pizzas');
});
and the same level view of pizzas.blade.php as welcome.blade.php on the views folder.
is there any other option to solve this problem except using laragon ?
Add these 2 blocks in your config like this:
server {
#
# your configs...
#
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ /\.ht {
deny all;
}
}
Restart your NGINX server.
(I'm not sure about your other configs you have, but I believe the first block is what you want.)

Laravel + NGINX CSS/JS 404 not found

Laravel 5.5 fresh
nginx version: nginx/1.10.3 (Ubuntu)
When I go to: http://<my-ip>/crm/ all is working great, I get the Laravel welcome page, all js and css are loading correctly.
When I go to http://<my-ip>/crm/register - I get 404 for css and js.
This is my conf:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
rewrite ^/((?!en)[a-z]*)/home$ /index.php?lang=$1&$args last;
rewrite ^/((?!en)[a-z]*)/sitemap.xml$ /sitemap.xml last;
rewrite /pigeon/(.*)$ /pigeon/index.php?/ last;
rewrite /crm/(.*)$ /crm/index.php?/ last;
if (!-f $request_filename){
set $rule_52 1$rule_52;
}
if ($rule_52 = "1"){
rewrite ^/(.*)(?<!php)$ /$1.php last;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /pigeon {
alias /var/www/html/pigeon/public;
try_files $uri $uri/ #pigeon;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
location /crm {
alias /var/www/html/crm/public;
try_files $uri $uri/ /index.php?$query_string;
location /crm.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
How do I fix this conf file to catch css and js files correctly?
Thanks
I had a similar issue. It seems allright what you have there in the server block.Although you might be better off if you add a proper server name.
Check this question and see if it helps. It has to do with the symlink to var/www/html for your crm, if you used one.
Laravel 8 + nginx - app.css and app.js resources from public/ not loading - 404 not found
And if not you could still use the online CDN's from bootstrap.Also in the question.

Nginx / Laravel config to redirect from non index.php route

I've just deployed a newly built website running Laravel 5 and nginx and struggling to setup my nginx config to 301 redirect some old URLs - specifically those with .php extensions.
I'm looking to 301 redirect the below...
domain.com/foobar.php?id=123 -> domain.com/foobar/123
The default config assumes everything runs from index.php, so Laravel does not trigger on foobar.php.
I have tried adding the following rewrite rule, but it doesn't trigger (I assume because nginx isn't listening out for foobar.php requests?
rewrite ^/foobar.php(.*)$ /foobar/$1/ permanent;
Relevant snippet of my current nginx config below...
server {
listen 443 ssl;
server_name domain.com;
root /home/domain.com/public;
index index.html index.htm index.php;
charset utf-8;
if ($request_method = GET ) {
rewrite ^([^.]*[^/])$ $1/ permanent;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/domain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
This should work:
rewrite ^/foobar.php /foobar/$arg_id/? permanent;
You can get any argument like this $arg_{your_argument}. Just replace {your_argument} with the actual argument name.

nginx / varnish / magento - 500 Error when changing port

I have installed varnish on nginx. I have some really big problems.
my default.vcl is:
backend default {
.host = "127.0.0.1";
.port = "81";
}
my website virtual server is:
server {
listen 80;
root /var/www/site.com.ro/public_html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name www.site.com.ro;
access_log /var/www/site.com.ro/logs/log.access;
error_log /var/www/site.com.ro/logs/log.error error;
location / {
index index.php;
try_files $uri $uri/ #handler;
}
location #handler {
rewrite / /index.php;
}
location /blog {
alias /var/www/site.com.ro/public_html_blog/;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php;
}
location ~ ^/blog(.+\.php)$ { ### This location block was the solution
alias /var/www/site.com.ro/public_html_blog/$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
if (!-e $request_filename) {
rewrite / /index.php last;
}
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
# Make site accessible from http://localhost/
server_name site.com.ro;
rewrite ^(.*) http://www.site.com.ro$1 permanent;
}
and the varnish file is:
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
when I try to change the listen port for the website to 81 I get a 500 error...
can anyone help me? I don't know what I did wrong.
Varnish is configured to listen to port 80, and connect to localhost:81. nginx is configured to listen to port 80. You didn't mention the intended flow, but I'm taking a wild shot and guessing:
client -> varnish:80 -> nginx:81.
Do you spot the problem now?
Hint:
server {
listen 80;
Oh, and make sure you have a real similar setup in a test machine (virtualbox - or something) that you use when you dabble with settings you're not familiar with. That will give you time to understand why something is not working, and will gain you invaluable experience so you don't have to mess up the production site(s).

Rewriting URI within Nginx

Im trying to get URI Routing set up for my framework and im currently working on nginx as teh server, but the problem is i keep getting 500 error when trying to access either one of the following links
http://localhost.framework/
http://localhost.framework/index.php/
If I access the site using the following links it works:
http://localhost.framework/index.php
http://localhost.framework/index.php?/
my configuration for the domain is as follows:
server {
listen 80;
server_name localhost.framework;
root /var/www/ASFramework;
access_log /var/log/nginx/framework.access.log;
error_log /var/log/nginx/framework.error.log;
location / {
rewrite ^/(.*)$ /index.php/$1 last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/ASFramework$fastcgi_script_name;
}
}
basically what im trying to do is take the following url
http://localhost.framework/controller/method/../
and rewrite it to:
http://localhost.framework/index.php/controller/method/../
Within the logs (error.log) is:
2011/07/03 22:57:22 [error] 19837#0: *6 rewrite or internal redirection cycle while processing "/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/", client: 127.0.0.1, server: localhost.framework, request: "GET / HTTP/1.1", host: "localhost.framework"
Can anyone tell me what's going on and how I can fix it?
Change this line:
location ~ \.php$ {
into this:
location ~ \.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/ASFramework$fastcgi_script_name;
}
Your rewrite rule causes redirection cycle. nginx substitutes index.php with index.php/index.php recursively. So after second replacement your new URL will be index.php/index.php/index.php and so on.
You probably want something like this:
location / {
rewrite ^/index.php\?action=(.*)$ /$1 last;
}
Which rewrites index.php?action=someaction to /someaction.
Try this:
location / {
if ($request_uri !~ "/(index\.php)") {
rewrite ^/(.*)$ /index.php/$1 last;
}
}

Resources