I want to redirect an old path to a new one without changing the url and keeping the suffix of the url for the redirection:
dev-osm.blah.com/NYC/v2/site/links?key=a12345
redirect to
dev-osm.blah.com/NYC/v1/site/links2?key=a12345
server {
server_name dev-osm.blah.com
.
.
location ^~ /NYC/v2/site/links {
rewrite ^ /NYC/v1/site/links2
}
Use something like the following in your server config
location /NYC/v2
proxy_pass: /NYC/v1/
Related
I am looking for a "full NGINX solution", withou intermediary redirection... But I need some external processing by "my Name-Resolver", as illustred with this execute fantasy:
server {
server_name resolver.mydomain.com;
execute xx = http://localhos:123456/myNameResolver/$request_uri;
rewrite ^ http://www.adifferentdomain.com$xx? permanent;
}
So, is possible to do something like this? perhaps using a kind of fastcgi_pass but only to return a string, not to bypass all HTTP resolution.
Well, you can use HttpLuaModule, which can execute commands and store them in variables if needed.
location / {
server_name resolver.mydomain.com;
# Get response via lua script.
set_by_lua_file $xx 'resolver-script.lua' $request_uri;
rewrite ^ http://www.adifferentdomain.com$xx? permanent;
}
You just need a Lua script to do your request for you, try something like this using your $request_uri as arg[1], because it is being considered as a command line argument
I'm new to Nginx and I'm trying to redirect a directory to a file, here is basically what I'm trying to do:
When entering this link:
http://localhost:8889/dir
go to this link instead:
http://localhost:8889/dir/path/to/the/file/index.html
As you can see the directory is part of the file path which I'm trying to redirect to. Plus there is already a location block in the config file to redirect that directory to the proper location:
location /dir {
root /opt/dir;
}
My first attempt was to use the rewrite directive as I saw from this blog(https://jeffsebring.com/2012/nginx-301-redirects/):
if ( $request_filename ~ dir ) {
rewrite ^ http://localhost:8889/dir/path/to/the/file/index.html permanent;
}
But the page says it has a redirect loop, which I believe is the conflict with the old location block.
Then I tried to add another location block as I saw from here(nginx rewrite virtual directory to file):
location /dir {
rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
But after reloading config file, Nginx told me there is already a location block with the same directory exists.
So my question is is there any way I can do this? or it is not even possible?
Thanks!!
Found the solution.
I used "return" to return the full hard-coded url instead rewrite the current one:
location ~* /dir$ {
return http://localhost:8889/dir/path/to/the/file/index.html;
}
And also this rewrite solution works as well:
location ~* /dir$ {
rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
The problem I had was actually caused by cache. I was actually reloading the page from cache so I could not see the result after changing nginx config file, and I resolved this by checking the "Disable cache" option in developer tools in Chrome.
I want to configure Nginx so that this URL:
http://www.example.com/x/...
redirects here:
http://x.example.com/...
and everything else (not http://www.example.com/x/...)
http://www.example.com/...
remains as-is:
http://www.example.com/...
How do you configure Nginx to do this?
you do os by setting up 2 location blocks inside your server config: 1 with the redirect, and 1 with the default stuff that matches anything else like so:
location / {
# directives needed to serve the regular stuff
}
location /x/ {
rewrite ^/x/(.*)$ $1 last;
}
Okay I'm kind of n00b on Nginx, and I've browsed through here and couldn't piece together an answer. SO here is what i got
server {
root /usr/share/nginx/www;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location /dojump {
rewrite ^/dojump/(.*)$ /dojump/index.php/$1 break;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
This is on a wordpress setup the first location block should pass all file requests to the wordpress bootstrap.
the location /dojump block is supposed to be for an outbound redirect script i have. I want to catch the arguments and pass them to the index.php script
like /dojump/cnn.com
to /dojump/index.php/cnn.com
it works with apache with this simple .htaccess line inside the dojumps folder
RewriteRule ^(.*)$ index.php/$1 [L]
however, I get an nginx error in the error log
/usr/share/nginx/www/dojump/index.php/cnn.com" failed (20: Not a directory)
Any help?
Thanks
Try passing the url as a GET parameter.
rewrite ^/dojump/(.*)$ /dojump/index.php?url=$1 break;
I'm serving /foo/bar/ by way of proxypass and want to continue doing so. However, I would like to serve /foo/bar/baz.swf statically from say /var/www/mystatic/baz.swf and so forth.
I was hoping that I could do something like
location /foo/bar/(.*) {
alias /var/www/mystatic/;
}
location / {
proxy_pass ....;
...
}
And /foo/bar/ would go to the application server while /foo/bar/(.*) would be served statically.
the docs say that I can't do this and need to use a combination of root and rewrite: http://wiki.nginx.org/NginxHttpCoreModule
Adding to the complication, I would like to continue using the ancient, unsupported 0.5.33. Any help would b greatly appreciated.
Edit: moving forward, someone suggested using root instead of alias. But, it doesn't seem that I can use any regex on the location directive with my version? Here, /foo/bar/baz.swf is served by the proxy_pass! I have the file at /var/www/foo/bar/baz.swf.
location /foo/bar/(.+) {
root /var/www/;
}
You can do this; but it's slightly esoteric. Try using:
location ^~ /foo/bar {
alias /var/www/mystatic/;
}
location / {
proxy_pass ....;
}
These options are documented on the Wiki http://wiki.nginx.org/NginxHttpCoreModule#location
location = /foo/bar/baz.swf {}
will clear clear any options set to /foo/bar/baz.swf. So you can leave it
where it is as the proxy options will not be used.
You can:
# mkdir /var/www/foo
# mv /var/www/mystatic /var/www/foo/bar
then use this config:
location ~ ^/foo/bar/(.+) {
root /var/www/;
}