How can I rewrite this nginx command with try_file in Nginx? - nginx

I want to hide jsp extension in url by Nginx. How can I rewrite this nginx command with try_file in Nginx?
location ~ .*\.jsp$ {
root /var/www/html/www.domain.com;
if (!-f $request_filename) {
rewrite ^/(.*)$ /index.jsp?q=$1;
break;
}

location ~ \.jsp {
try_files $uri /index.jsp?q=$request_uri;
}
http://wiki.nginx.org/IfIsEvil

Related

How to edit url without updating in browser - nginx config

I want the Request URL to be converted from
http://host.com/newname/abc?def= to
http://newname/abc?def=
here is the config file
server {
listen 80;
server_name
host.com
location / {
rewrite ^(.+)/$ $1 permanent;
rewrite ^(.+)/index\.html$ $1 permanent;
rewrite ^(.+)\.html$ $1 permanent;
try_files /$host/public/$uri #webserver;
}
}
Adding above line worked form me
location / {
rewrite ^ $scheme://$request_uri? permanent;
}
But it replaces the url in user browser which i donn't want to happen.
Any way to achieve it
You can try this:
location /newname{
proxy_pass http://example.com/newname/abc?def=;
}
or
location /newname{
proxy_pass http://newname/abc?def=;
}

How to convert url segment to querystring with Nginx rewrite

I whould like this simple rewrite rule:
http://example.com/8743b52063cd84097a65d1633f5c74f5?param1=999&param2=2222
to be redirected to:
http://example.com/index.php?param1=999&param2=2222&hash=8743b52063cd84097a65d1633f5c74f5
The following is my default location:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
How can I achieve this using Nginx rewrite?
Using a rewrite statement:
rewrite "^/(\w{32})$" /index.php?hash=$1 last;
Or, within a location block:
location ~ "^/(?<hash>\w{32})$" {
rewrite ^ /index.php?hash=$hash last;
}

nginx location directive not working as expected

The following configuration will rewrite /admin while proxy_pass /core, I can't figure out the reason. Any hint on this? Similar case like this Nginx: location regex for multiple paths with backend .
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /\#$1 break;
}
}
location ~ ^/(admin|core)/ {
proxy_pass http://127.0.0.1:8080/$1;
}
Using try_files will simplify things.
location /
try_files $uri $uri/ =404;
}
location ~ ^/(?:admin|core)/ {
proxy_pass http://127.0.0.1:8080;
}
Try this variant:
location ~ ^/(admin|core)(.*)$ {
proxy_pass http://127.0.0.1:8080/$1;
}

nginx try_files, pushState, and 404

I'm trying to configure nginx to work with a pushState-enabled app. I've got it mostly working, but what I'm missing is getting a 404 returned on non-existent documents. How can I achieve this? Relevant part of my nginx.conf:
location / {
root /foo;
index index.html;
try_files $uri /index.html;
}
I've tried try_files $uri /index.html =404;, but that didn't work, obviously, because index.html exists.
I've also tried...
if (!-e $request_filename) {
return 404;
}
...in the server and location block. Neither worked.
Try this. If file exists, redirect to index.html. Otherwise return 404.
location = /index.html {}
location / {
if (!-f $request_filename) {
return 404;
}
rewrite ^/(.*) /index.html redirect;
}

Nginx rewrites static files to index.html

I'm trying to work on a single page app - I need to rewrite all urls to index.html but allow existing static files (.css and .js) to be served as they normally would be in a browser.
This is the code that I'm trying to use to re-write but it serves my static files to the re-write as well
if (!-e $request_filename)
{
rewrite ^/(.*)$ /?/$1 last;
break;
}
you don't actually need a rewrite for that in nginx, just use try_files like so:
location / {
try_files $uri /index.html;
}
what this does is for all url's:
try the exact static filename match, and serve it if present
if 1 didn't serve anything, then server /index.html instead
see http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
This should work:
server {
listen 1.2.3.4:80;
server_name domain.eu;
root /usr/local/www/domain.eu/public;
try_files $uri #rewrites;
location #rewrites {
rewrite ^/favicon.ico$ /pictures/favicon.ico last;
rewrite ^ /index.html last;
}
}

Resources