Pinnect's htaccess rules to nginx - nginx

so I've been playing around with the rules trying to transform them, the pinnect script's rules are:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
#RewriteBase /
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
# fix for uploadify 302, 406 errors
SetEnvIfNoCase Content-Type "^multipart/form-data;" "MODSEC_NOPOSTBUFFERING=Do not buffer file uploads"
I have pinnect on pinnect/ on the server, however VB is running on the index for now,
I've tried
location /pinnect {
try_files $uri $uri/ /index.php;
}
And different variations of that, however let's say I go to the /install directory - it tries to redirect me to pinnect/forums.php(because of VB), if I try to go to index.php directly, an endless loop occurs, anyone could give me a hand with this?
EDIT:
So i've got this rule currently
location ~ ^/pinnect/index.php { try_files maintenance.html #index.php; }
location ~ ^/pinnect/ {
rewrite ^/pinnect$ /pinnect/index.php last;
rewrite ^/pinnect/$ /pinnect/index.php last;
#rewrite ^/pinnect/([^.]+)$ /pinnect/index.php/$1 last;
rewrite ^/pinnect/(.*) /pinnect/index.php/$1 last;
}
However where ever I go, I get a 301, 302 so it's not behaving same way as the htaccess, could anyone help?

If it's just a matter of status code returned, you might have to use return instruction on your location block.
location ~ ^/pinnect/index.php { try_files maintenance.html #index.php; }
location ~ ^/pinnect/ {
rewrite ^/pinnect$ /pinnect/index.php last;
rewrite ^/pinnect/$ /pinnect/index.php last;
#rewrite ^/pinnect/([^.]+)$ /pinnect/index.php/$1 last;
rewrite ^/pinnect/(.*) /pinnect/index.php/$1 last;
return 200;
}
Read more…
As we are here, you should replace last by break since you have your instructions in a location block.
Also, you might prefer using ? instead of (.*) to drop the original arguments.
Read more…

Related

How do I properly add try_url or rewrite for a .html add?

So, coming from the Apache world I am new to configuring NGNIX more than just the root site. I have tried to use an Apache to NGNIX rewrite, but the output doesn't seem to render the pages correctly.
Original .htaccess code:
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
The output doesn't seem to work after reloading the new configuration.
I have been trying different configurations under my location block trying to just add .html and end of the URI/URL. This is for a sub-directory called "wiki" just serving static HTML files.
Here's my current configuration under HTTPS:
root /var/www/html/;
index index.php index.html;
...
location ^~ /wiki/ {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^(.+)/$ $1.html permanent;
}
The alias or path is /var/www/html/wiki
Please advise. I was able to get /wiki/index.html to render, but any other HTML files do not rewrite from wiki/product to product/product.html. I keep getting 404 errors.

Lumen in a subfolder trailing slashes issue

In public/index.php I have changed $app->run() to $app->run($app['request']) and this resulted in Lumen working in a sub-folder, so this works:
http://local.dev/app/
http://local.dev/app/test
However if there's a slash at the end of a route then I get a NotFoundHttpException. For example:
http://local.dev/app/test/
I'm using NGINX and my rewrite rule for the folder is:
location /app/ {
try_files $uri $uri/ /app/index.php?$query_string;
}
Am I doing something wrong?
If laravel is in a sub-directory use this :
RewriteRule ^(.*)/$/ /$1 [L,R=301]
instead of this:
RewriteRule ^(.*)/$ /$1 [L,R=301]

How to rewrite based on accept heads in nginx

I recently switched over to nginx and am fairly new at it, so forgive me if this has been covered extensivly before.
What im trying to do is rewrite the user request based on tthe accept header sent out.
In paticular:
if the accpet header is an image/gif or image/webp then serve the image, if not concat off the .gif of the url and serve that.
hears my apache configuration, but again im on nginx now and trying to learn how i could convert it over:
RewriteCond %{HTTP_ACCEPT} ^image/gif [NC]
RewriteCond %{HTTP_ACCEPT} ^image/webp [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com(.*)$ [NC]
RewriteRule ^i/(.*)\.gif http://example.com/i/$1 [R=302,L]
as you can see the above htaccess file works like a charm, but nginx is completly different it seems.
Ive done some reading and come up with this:
map $http_accept $webp_suffix {
default "";
"~*webp" ".webp";
}
with the following inside the server block
location ~* ^/i/.+\.(gif)$ {
root /storage-pool/example.com/public;
add_header Vary Accept;
try_files $uri$webp_suffix $uri =404;
}
Sadly, this doesnt work and I still dont know how to trouble shoot in nginx either.
Any information would be greatly appreciated, thanks.
From your location, the try_files will concat $uri with $webp_suffix which might not what you want, for example if you have a request to /i/test.gif with HTTP_ACCEPT header set to image/webp your location config above will try to find files at:
/storage-pool/example.com/public/i/test.gif.webp
You probably want something like this below instead:
location ~* ^(?P<basename>/i/.+)\.gif$ {
root /storage-pool/example.com/public;
add_header Vary Accept;
try_files $basename$webp_suffix $uri =404;
}
This location will capture the image path without the .gif suffix and it will be available as variable named $basename
More information about named capture here: http://nginx.org/en/docs/http/server_names.html#regex_names

root defaults to welcome to Nginx when using a "greedy" location

It seems like similar questions have been asked consistently on the site, but despite of browsing dozens of similar questions, none of the solutions seem to work for me.
When I go to mydomain.com, then nginx shows the default "welcome to nginx" page, however, when I visit mydomain.com/whatever/blablabla/whatever then Nginx sends the user to index.php?q=$uri which is the expected behavior.
So the problem only happens when visiting the actual root domain: mydomain.com.
This is my default file:
server {
listen 80;
location / {
include /etc/nginx/include/php;
index index.php;
root /var/www/mydomain.com/current/web;
}
location ~ /(.*)/? {
try_files $uri $uri/ /index.php?$uri;
}
}
Under that configuration, visiting mydomain.com defaults to "welcome to nginx" page, but mydomain.com/whatever is sent to index.php as expected.
When I tweak that to, something like:
location ~ /^(.*)/?$ {
try_files $uri $uri/ /index.php?$uri;
}
or
location ~ /^.*/? {
try_files $uri $uri/ /index.php?$uri;
}
then mydomain.com starts working again, but mydomain.com/whatever starts showing a 404 page. I've tried a bunch of different configurations, but none of the configurations I've tried seem to fully work as I expect.
Basically what I want is pass the whole URL to a front controller, to parse it and take actions depending on the contents of such URL.
If it helps, I want to mimic in Nginx this behavior from Apache:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
I also have another virtual host:
server {
listen 0.0.0.0:443;
root /var/www/mydomain.com/current/web;
index index.html index.php;
fastcgi_param HTTPS on;
include /etc/nginx/include/ssl;
include /etc/nginx/include/php;
}

Nginx + rewrite + php-fpm = confusion

I'm moving from Apache to Nginx. I've got problem with converting Apache rewrite rules into nginx rules. What I'm trying to convert:
RewriteRule ^$ www/controller.php?_url_=index [QSA,L]
RewriteRule ^/+$ www/controller.php?_url_=index [QSA,L]
RewriteRule ^([a-zA-Z0-9_]+)(/([a-zA-Z0-9_/]*))?$ www/controller.php?_url_=$1&_req_=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9/]+)controller.php?(.*)$ www/controller.php?$2 [QSA,L]
What I tried to use:
rewrite ^/$ /www/controller.php?_url_=index break;
rewrite ^/+$ /www/controller.php?_url_=index break;
rewrite ^/([a-zA-Z0-9_]+)(/([a-zA-Z0-9_]*))?$ /www/controller.php?_url_=$1&_req_=$2 break;
rewrite ^/([a-zA-Z0-9/]+)controller.php?(.*)$ /www/controller.php?$2 break;
If I use above rules my browser is downloading php file (server is not executing it) - I guessed it's not being passed to PHP-FPM. Somewhere I found I should replace "break;" with "last;" like:
rewrite ^/$ /www/controller.php?_url_=index last;
After replacing this still I'm downloading php file from http://example.org, but when I visit http://example.org/login I get into infinite loop. I read nginx documentation and different examples (also here at StackOverflow) but sill I can't find correct configuration. Could somebody point me into the right direction?
Here is my whole config file:
server {
listen 80;
server_name 10.10.100.172;
error_log /var/log/nginx/example.com.error.log debug;
rewrite_log on;
location / {
root /var/www/webs;
index index.php index.html index.htm;
rewrite ^/$ /www/controller.php?_url_=index last;
rewrite ^/+$ /www/controller.php?_url_=index last;
rewrite ^/([a-zA-Z0-9_]+)(/([a-zA-Z0-9_]*))?$ /www/controller.php?_url_=$1&_req_=$2 last;
rewrite ^/([a-zA-Z0-9/]+)controller.php?(.*)$ /www/controller.php?$2 last;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /var/www/webs;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EDIT:
I moved rules outside location segment and used "break;" at the end of each rule. I can reach /www/controller.php?url=login&req=/ when I go to example.org/login/ - controller.php was responsible for infinite loop. When I try to reach example.org or example.org/ I'm downloading controller.php file - like it's not being passed to the PHP-FPM. Any guess?
I used above rules outside location segment and it works! I tried viewing my page in different browser and ecerything is fine. I always forget about removing cache..

Resources