I need to redirect client.mysite.com to https while client.mysite.com/admin should keep http. How can I achieve that on nginx web server ?
right now I use this for subdomain but redirects client.mysite.com/admin too :
server {
listen 80;
server_name my.domain.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
You need to do the redirection conditionally only on location /
server {
listen 80;
server_name sub.example.com;
location = / {
return 301 https://example2.com$request_uri;
}
location / {
# remaining rules
}
}
Related
I have multiple domains as below: www.domain1.com www.domain2.fr www.domain3.com www.domain4.fr www.domain5.biz
I wanted to redirect specific requests coming to the any domain let's say traffic coming to www.domain1.com with specific parameter should be proxy_pass to domain1.domainabc.com
upstream backend {
server <server1 IP>:80;
server <server2 IP>:80;
}
server {
listen 80;
server_name www.domain1.com
server_name www.domain2.fr
server_name www.domain3.com
server_name www.domain4.fr
server_name www.domain5.biz
location / {
common confs
proxy_pass http://backend
}
location ~*/abc-xyz(.*) {
proxy_pass https://$domain.domainabc.com/abc-xyz/$1;
}
location ~/images/(.*) {
proxy_pass https://$domain.domainxyz.com/images/$1;
}
}
where $domain can be domain1, domain2, domain3 etc based on request coming to respective domains...
your earliest help would be apricated.
I tried multiple proxy_pass directtive but could not make it work.
I am trying to do a proxy_pass to a subdirectory http://ms.server.com:8085/ms. So whenever anyone hit on http://ms.example.com' it should be redirected tohttp://ms.example.com/ms`. I am trying to do that through below configuration
upstream example {
server ms.server.com:8085;
}
server {
server_name ms.example.com;
location / {
proxy_pass http://example/ms;
}
}
Now I am redirecting to "Nginx Test page".
proxy_pass is used to inform Nginx where to send proxied requests at a host level, and does not account for the URI.
What you want to use instead is a return statement, like so:
upstream example {
server ms.server.com:8085;
}
server {
server_name ms.example.com;
location = / {
return 301 http://ms.example.com/ms;
}
location / {
proxy_pass http://example;
}
}
This will redirect requests to the root URL of the server_name http://ms.example.com using a 301 redirect, and all other traffic will be passed through to the defined upstream.
I am trying to force HTTPS for incoming connections while also redirecting all requests to a certain url.
Desired result:
http://example.com -> https://example.com/dir
https://example.com -> https://example.com/dir
Here is what I believe should work but is saying there are too many redirects.
server {
listen 80;
listen 443 ssl;
server_name example.com;
location / {
return 301 https://$server_name/dir$request_uri;
}
location /dir {
try_files $uri $uri/ /index.php?$args;
}
Any help is much appreciated!
Server that is listening to 443 port shouldn't redirect to itself (return 301 https://...)
Can you try this,
server {
listen 80;
listen 443;
server_name example.com;
location / {
return 301 https://example.com/dir$request_uri;
}
location /dir {
...
...
}
}
Basically the idea is to keep both listen directive in the same server block. The above code is just an excerpt and you need to fill in the missing pieces.You can refer the return directive section of this link for more detail
EDIT: Updated the code with location block.
I've encountered an issue during server configuration: I require a 301 redirect from http://www.example.com to http://example.com just for one specific url alias - like /partners.
the expected output- http:// www.example.com/partners/stuff -> http:// example.com/partners/stuff.
I've tried adding the following code to the vhosts already:
server {
server_name http://www.example.com/partners;
return 301 $scheme://example.com/partners;
}
but vhosts gives me an error telling me this code isn't valid.
What's the correct way of implementing such rewrite?
server_name is for domain only. I can suggest you 2 solutions.
Copy configs between servers. This is the best solution recommended by nginx's author.
server {
server_name example.com;
include example.com.conf;
}
server {
server_name www.example.com;
include example.com.conf;
location /partners/ {
return 301 $scheme://example.com$request_uri;
}
}
Or using if. Bad solution due performance
server {
server_name .example.com;
...
location /partners/ {
if ($host = "www.example.com") {
return 301 $scheme://example.com$request_uri;
}
}
}
http://wiki.nginx.org/IfIsEvil
http://wiki.nginx.org/Pitfalls#Server_Name
How can I redirect mydomain.example and any subdomain *.mydomain.example to www.adifferentdomain.example using Nginx?
server_name supports suffix matches using .mydomain.example syntax:
server {
server_name .mydomain.example;
rewrite ^ http://www.adifferentdomain.example$request_uri? permanent;
}
or on any version 0.9.1 or higher:
server {
server_name .mydomain.example;
return 301 http://www.adifferentdomain.example$request_uri;
}
server {
server_name .mydomain.example;
return 301 http://www.adifferentdomain.example$request_uri;
}
http://wiki.nginx.org/HttpRewriteModule#return
and
http://wiki.nginx.org/Pitfalls#Taxing_Rewrites
Why use the rewrite module if you can do return? Technically speaking, return is part of the rewrite module as you can read here but this snippet is easier to read imho.
server {
server_name .domain.com;
return 302 $scheme://forwarded-domain.com;
}
You can also give it a 301 redirect.
I'm using this code for my sites
server {
listen 80;
listen 443;
server_name .domain.example;
return 301 $scheme://newdomain.example$request_uri;
}
That should work via HTTPRewriteModule.
Example rewrite from www.example.com to example.com:
server {
server_name www.example.com;
rewrite ^ http://example.com$request_uri? permanent;
}
If you would like to redirect requests for domain1.example to domain2.example, you could create a server block that looks like this:
server {
listen 80;
server_name domain1.example;
return 301 $scheme://domain2.example$request_uri;
}
You can simply write a if condition inside server {} block:
server {
if ($host = mydomain.example) {
return 301 http://www.adifferentdomain.example;
}
}
Temporary redirect
rewrite ^ http://www.RedirectToThisDomain.example$request_uri? redirect;
Permanent redirect
rewrite ^ http://www.RedirectToThisDomain.example$request_uri? permanent;
In Nginx configuration file for specific site:
server {
server_name www.example.com;
rewrite ^ http://www.RedictToThisDomain.example$request_uri? redirect;
}