Wildcard nginx rewrite - nginx

I suspect this will be quite simple, yet I can't seem to get my head around the nginx rewrite system. Hopefully someone could chime in and help me out?
Quite simply, I need..
http://example.com/?c=foo
to to be rewritten to...
http://foo.example.com
Thank-you in advance if you've taken pitty on this nginx noob :)
I'll keep having a go at this, and in the unlikely event I hit a 'eureka' moment, I'll update this to help anyone who's having/will ever have this similar 'problem'.
Cheers.

Actually, it is impossible to make internal redirect to other domain in Nginx. But there are two available solutions:
1) Simple: make redirect to specified address. Example:
if ( $arg_c ) {
rewrite ^ http://$arg_c.example.com;
}
2) Complex: if you use Nginx as a proxy between php-fpm, apache or something else, you could set the Host header in this way::
set $new_host example.com;
if ( $arg_c) {
set $new_host $arg_c.example.com;
}
and pass it as a parameter:
proxy_set_header Host $new_host;

Related

I can't redirect my domain to a subdomain using nginx location

I've a problem I tried to fix but I need help. I'm going to try to explain you what I need to do:
I have a nginx web server and I need to add a location to redirect my main page example.com to web.example.com but just this page. For anothers pages I need to use example.com/path.
For example any request to example.com it needs to redirecto to web.example.com (I can do that).
My problem start when I have a request to example.com/path and my location redirect to web.example.com/path that is not what I need.
I tried with some regular expresion but it does't work, any ideas?
Thank you!
If you only need a redirect for the main page try exact match location
location = / {
return 302 https://...$args;
}

Routing error with nginx when deploying app

This must be very common knowledge I can't seem to get. I don't even know the keywords to explain this. I'll use what I understand till now. So here is the situation I'm in,
localhost:5000 ('/') takes to a base page. I set localhost/api as location in nginx to the same port. So going to localhost/api is the same thing as going to localhost:5000.
Now as usual, some UI is there with some text box and different buttons. Normally, form-action for some button goes to ('/run') endpoint and the process continues. But after setting nginx, I expected this localhost/api/run in url box of browser but got localhost/run instead. This is the issue.
Here what I added to /etc/nginx/sites-available/default
location /api {
proxy_pass http://127.0.0.1:5000/;
}
I must tell you I know little to nothing about nginx (you already got that I guess). I just know what it is use for. I'd really appreciate a quick and general solution to this, and if someone could direct me to a playground for learning nginx would be wonderfull.

Nginx redirect to different domain only for specific query

I'm basically trying to have nginx redirect:
https://website1.com/program_video/programXXX.mp4
to
http://website2.com/ProgramsVideos/programXXX.mp4
Given that "XXX" is the id of the video and might change.
I've been wandering around stackoverflow and nginx directory but wasn't able to get anything to work.
Last thing I tried was:
rewrite "https://website1.com/program_video/([a-zA-Z0-9]{23})\.mp4)$" http://website2.com/ProgramsVideos/$1 permanent;
But it doesn't redirect anything...
Any help would be much appreciated.

Nginx rewrite rule guide

I am totally newbie on NGINX reverse proxy solution and its seems to getting hard to understand all the terminology. I am looking for a solution as follows. I would really appreciate anybody's help to configure the same.
We have an internal web server which we would like to publish at WWW site but don't want customer to see internal server URL. As example:
Customer access www.mycompany.com/track --> NGINX read the track and then redirect URL to internal server.com.au/tracker . We dont want customer to see this address.
Any suggestion?
Cheers,
Sandy
It is much better in this case to use the proxy. Rewrites are meant for URLs within the same domain or to redirect the client (which would show in the url).
Try this:
location / {
proxy_pass http://internal.example.com/;
proxy_set_header Host $host;
}
Rewriting on nginx is quite the same than on Apache :) The syntax varies.
When Apache uses RewriteRule, nginx uses rewrite. May I suggest you this reference http://wiki.nginx.org/HttpRewriteModule ? Check 2.4 and 2.5 for rewriting-specific documentation. You'll find information about the rewrite syntax, and rewriting options.
There is a quick example from above, if you just need the basic syntax :
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
By the way, if you want to work from examples, you can transform your Apache .htaccess file into a piece of nginx configuration, using this tool : http://winginx.com/htaccess

nginx 301 permanent redirect

I'm having a difficult time figuring out how should I make a hole bunch of redirects (301) to Nginx. Maybe you can give me some help.
Basically I have a few hundred of url's like this one above:
http://www.website.com/product/intl/bg-BG/browserchoice/download.html?track=1871
And I need to redirect to:
http://www.website.com/home/download/download.php?prod=browser&track=1871
How should I do it? (*bg-BG from source is the single variable that changes)
Please help me.
Thanks.
Please try this, it peforms internal redirect in nginx:
rewrite ^/product/intl/.*/browserchoice/download.html$ /home/download/download.php?prod=browser&$args break;
But if you really want redirect (this is not a good choice) :
rewrite ^/product/intl/.*/browserchoice/download.html$ http://www.website.com/home/download/download.php?prod=browser&$args permanent;
I also suggest you to read official documentation here http://wiki.nginx.org/HttpRewriteModule

Resources