nginx conditional rewrite issue - mod-rewrite

location / {
try_files $uri $uri/ /index.php?$args =404;
rewrite ^/(\w+)$ /?system=$1 break;
}
This block rewrites /first to /?system=first, /second to /?system=second, etc.
However, this rewrite should not be done for /six and /nine. How could I write this condition?

Fixed with regex
rewrite ^/((?!six|nine)\b\w+$) /?system=$1 break;

Related

NGINX return 404 error when adding a trailing slash

Today, I have a problem with NGINX.
In my NGINX config I have this :
location ~ / {
try_files $uri $uri/ /index.php?$query_string;
}
For example, I want to access https://my.domain/hello with a route defined with Laravel. It works
but now if I access https://my.domain/hello/ it returns a 404 NGINX error page. I also point out that I use Plesk.
Do you have any ideas on how to fix that ?
Thank you for your time ;)
Add 301 redirect if path is not directory and ending on /
if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
If you check the nginx config template you will find this line
/usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php
<?php if ($VAR->domain->physicalHosting->directoryIndex && !$VAR->domain->physicalHosting->proxySettings['nginxProxyMode']): ?>
location ~ /$ {
index <?=$VAR->quote($VAR->domain->physicalHosting->directoryIndex)?>;
}
<?php endif ?>
You either need to reenable proxy mode (it will reenable apache but nginx will still be receiving the request in the first place) or create a custom template for your domain and delete this line
This will not concern you, but if you were not using php (eg: nodejs), you could also disable php support, which would also get rid of this line

nginx rewrite rule conflict

I am having a conflict with two blocks with nginx 1.8.0.
The first block is to setup static cache for certain file types:
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|swf)$ {
add_header "Access-Control-Allow-Origin" "*";
access_log off;
log_not_found off;
expires max;
}
The second block is a series of rewrites defined by filetype:
location /files {
rewrite ^/files/master\.([0-9]+)?\.css$ /min/?g=css&456 break;
rewrite ^/files/master\.([0-9]+)?\.js$ /min/?g=js&456 break;
rewrite ^/files/second\.([0-9]+)?\.js$ /min/?g=jsa&456 break;
}
The rewrites result in a 404. Any rewrite that uses a filetype defined in the static cache rule results in a 404 error. If I change the rewrite rule to a different filetype or comment out the static cache file block, it works.
What am I missing in the cache static files that is preventing a rewrite from being performed at a later config setting?
After much gnashing of teeth, I ended up changing the redirects to a try_files parameter. The parameters must be higher in the conf file than the static cache file.
location ~ ^/files/master\.([0-9]+)?\.css$ {
try_files $uri /min/?g=css&456;
}
location ~ ^/files/master\.([0-9]+)?\.js$ {
try_files $uri /min/?g=js&456;
}
location ~ ^/files/second\.([0-9]+)?\.js$ {
try_files $uri /min/?g=jsa&456;
}
This will allow me to run the minify toolset.

nginx redirect all directories except one

I'm using nginx 1.0.8 and I'm trying to redirect all visitors from www.mysite.com/dir to google search page http://www.google.com/search?q=dir where dir is a variable, however if dir=="blog"( www.mysite.com/blog) I just want to load the blog content(Wordpress).
Here is my config :
location / {
root html;
index index.html index.htm index.php;
}
location /blog {
root html;
index index.php;
try_files $uri $uri/ /blog/index.php;
}
location ~ ^/(.*)$ {
root html;
rewrite ^/(.*) http://www.google.com/search?q=$1 permanent;
}
if I do this even www.mysite.com/blog will be redirected to google search page. If I delete the last location www.mysite.com/blog works great.
From what I've read here: http://wiki.nginx.org/HttpCoreModule#location it seems that the priority will be first on regular expressions and that first regular expression that matches the query will stop the search.
Thanks
location / {
rewrite ^/(.*)$ http://www.google.com/search?q=$1 permanent;
}
location /blog {
root html;
index index.php;
try_files $uri $uri/ /blog/index.php;
}
http://nginx.org/r/location
http://nginx.org/en/docs/http/request_processing.html
This situation can also be handled using only regex. Though this is a very old question and it has been marked answered, I'm adding another solution.
If you use multiple loop forwards using reverse proxy this is the easiest way without having to add a separate location block for every directory.
root html;
index index.php;
location / { #Match all dir
try_files $uri $uri/ $uri/index.php;
}
location ~ /(?!blog|item2)(.*)$ { #Match all dir except those dir items skipped
rewrite ^/(.*) http://www.google.com/search?q=$1 permanent;
}

nginx rewrite rule with try_files

At the bottom of this post is my current nginx rewrite rule and for the most part it is working as it should. The only issue I seem to be coming across is in the example of a link such as example.com/?action=cmd, this action is being processed as example.com/clientarea/?action=cmd
I am looking for a way that when the ? symbol is called that it will automatically append it to example.com/index.php instead of /clientarea/
location = / {
rewrite ^ /clientarea/ permanent;
}
location ~ ^(.*)$ {
try_files $uri $uri/ /index.php?$1;
}
Assuming you have a recent version of Nignx 1.0.x+ (I think), this should work:
location = / {
if ($is_args = '?') {
return 301 $scheme://$host/index.php$is_args$args;
}
rewrite ^ /clientarea/ permanent;
}
location ~ ^(.*)$ {
try_files $uri $uri/ /index.php?$1;
}

How do I rewrite mysite.com/page/ to mysite.com/page/index.html with NGINX?

How can I 301 rewrite mysite.com/page/ to mysite.com/page/index.html using nginx?
In Apache I had:
RewriteRule page/ http://mysite.com/page/index.html [R=301,L]
Thanks for help,
hydn
Try this settings:
location / {
rewrite /page/ http://mysite.com/page/index.html permanent;
...
}
I see from your comment to Sergiei that the '/page/' directory and '/page/index.html' does not actually exist and is rewritten elsewhere. So not surprising that Nginx gives a 404 not found.
What exactly should get served if a visitor requests '/page/index.html'? I.E., what does that get rewritten to?
If it is index.php?q=/page/index.html, then your config should be:
server {
# index directive should be in server or http block
# So no need to repeat in every location block
# unless specifically overriding it
index index.php index.html;
location / {
rewrite ^/page(/?)$ /page/index.html break;
try_files $uri $uri/ /index.php?q=$uri;
}
}
You could also use
server {
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
}
But there may be some issues with that. All depends on the detail of your application.

Resources