I have used return or rewrite in Nginx to redirect, but both will redirect with the http header Location which has value of the full url, but I want it just value the path.
for example:
return 301 /test;
or
rewrite ^/$ /test permanent;
will has the Location value of
http://www.example.com/test
but I want it be just
/test
I wonder if it is possible.
Solved.
as of 1.11.8, just set the directive(Thanks to #RichardSmith):
absolute_redirect off;
before 1.11.8, I find a wonderful answer(Thanks to #xiaochen):
https://stackoverflow.com/a/39462409
Related
I want to redirect http to https and remove trailing slashes in nginx with one single redirect. The solution I have today is the following:
server {
listen 80;
server_name www.example.com
rewrite ^/(.*)/$ /$1 permanent;
return 301 https://$host$request_uri;
}
The problem with this solution is that it will give two redirects
Gives Two redirects:
http://www.example.com/test/ --> http://www.example.com/test
http://www.example.com/test --> https://www.example.com/test
Is it possible to make a solution where you only get one single redirect like bellow?
http://www.example.com/test/ --> https://www.example.com/test
when I looked through the documentation of nginx rewrite and return methods I felt like it should be possible to do it with a single rewrite somehow:
rewrite ^/(.*)/$ https://$host$request_uri permanent;
But nothing I have tried have given me the correct results.
You already had the components of a correct solution. Use the scheme and hostname, together with the capture to construct the destination URL:
rewrite ^/(.*)/$ https://$host/$1 permanent;
I'm trying to remove trailing slash on http post method, when i try to re-write the URL using rewrite ^/(.*)/$ /$1 permanent; it doesn't work for me
The upstream should receive in this format /x/y if the Http POST is coming in these format
location /x/y/ ==> location /x/y
location /x/y ==> location /x/y
This is the nginx configuration
upstream backend {
server 127.0.0.1:8778;
# Number of idle keepalive connections per worker process.
keepalive 35;
}
location /x/y {
limit_except POST {
deny all;
}
proxy_pass http://backend;
proxy_buffering on;
include proxy.conf;
}
The problem here is when the upstream see the URI is in this format /x/y/ it rejected the request, what should be the correct rewrite rule for this so that if the http post comes in the format like /x/y or /x/y/ the upstream should always see /x/y
The permanent will cause the rewrite to generate a redirect with a 301 response. What you need is an internal adjustment of the URI before sending it upstream:
location /x/y {
rewrite ^/(.*)/$ /$1 break;
...
}
See this document for more.
May I ask Where and How to redirect
http://domain.com, http://www, https://domain.com
to
https://www
?
Where
In your Nginx config file (the main or vhost depending on your setup)
How
Try rewrite:
server {
listen 80;
server_name www.domain.com domain.com;
rewrite ^ https://www.doamin.com$request_uri? permanent;
}
or return:
server {
listen 80;
server_name www.domain.com domain.com;
return 301 https://www.domain.com$request_uri
}
Choice is your when it comes to Return vs Rewrite:
REWRITE
Only the part of the original url that matches the regex is rewritten.
Slower than a Return.
Returns HTTP 302 (Moved Temporarily) in all cases, irrespective of permanent.
Suitable for temporary url changes.
RETURN
The entire url is rewritten to the url specified.
Faster response than rewrite.
Returns HTTP 301 (Moved Permanently).
Suitable for permanent changes to the url.
No need to set permanent.
Officila Nginx Docs on return/rewrite.
I'm new to nginx configs and have spent a lot of time googling so far. I'm trying to create a very basic nginx config file to be used in a "redirect" server.
Users will be required to point naked domains (example.com) by A-record to my redirect server IP address, and the 'www' record by CNAME to another server.
The purpose of the redirect server is to then perform a 301 redirect any/wildcard naked domains back to to the 'www' version of the domain so it can be properly handled by my other server.
But I also want to catch any misconfigured 'www' domains that are pointing to my server IP by A-record, and simply direct them to a custom error page on the redirect server with further instructions on how to set up their account correctly for my service.
Here's what I have. It works, but since I am new to writing configs I was wondering if there is a better way to handle the redirect to the custom error page in the first server block. TIA!
#redirect to error page if begins with 'www.'
server {
listen 80;
server_name ~^www.; #only matches if starts with 'www.'. Is this good enough?
rewrite ^(.*)$ /404.html; #is this the correct way to direct to a custom error page?
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
}
#no match, so redirect to www.example.com
server {
listen 80 default_server;
rewrite ^(.*)$ $scheme://www.$host$1 permanent;
}
Prefix/suffix server name matching is faster and easier than regexp.
Also, there is no reason to use rewrite. You want to return 404, so do it and nginx will do all the rest. BTW, with rewrite you will return 200 OK with content of /404.html instead of 404 Not Found.
So here it is:
server {
listen 80;
server_name www.*;
root /usr/share/nginx/html;
error_page 404 /404.html;
location / {
return 404;
}
location = /404.html {
internal;
}
}
Yet another nginx redirect question.
I've been trying to redirect domain.com/blog/post-1, /blog/post-2 to sub.domain.com/blog/post-1, etc.
Any pointers?
If you don't want to serve anything on domain.com without subdomains, add this block:
server {
server_name domain.com;
return 301 $scheme://sub.domain.com$request_uri;
}
If you want to use it somehow, add this to your domain.com server block:
location /blog {
rewrite ^ http://sub.domain.com$request_uri? permanent;
}
Of course, in any case you want sub.domain.com server block which is catching this request.