I want to handle this:
blog.example.com => example.com/blog
blog.example.com/xxx => example.com/blog/xxx
in both samples there is nothing in blog subdirectory, and the code which should handle the blog is in subdomain. and just i want to show the url as showed above.
so. i want to forward(redirect without changing url) a subdirectory to subdomain.
is there any nginx configuration to do that?
You could have the following in your NGINX configuration.
server {
listen 80;
server_name blog.example.com;
location / {
return 301 $scheme://example.com/blog$request_uri;
}
}
server {
listen 80;
server_name example.com;
location /blog/ {
<your code goes here>
}
}
This takes any incoming requests to blog.example.com and redirects to example.com/blog along with the requested URI eg. blog.example.com/latest would redirect to example.com/blog/latest
location = /blog {
return 302 /blog/;
}
location /blog/ {
proxy_pass http://blog.example.com/;
}
Note that the / in proxy_pass is very important (without it the /blog part won't be stripped out in the request to upstream).
Some further details on the rationale of two independent location statements are available at https://serverfault.com/questions/562756/how-to-remove-the-path-with-an-nginx-proxy-pass/562850#562850.
Related
Nginx is new to me. I'm currently redirecting http to https using the following.
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
How would I do the same except not redirect anything going to subfolder /abcd/ ?
I've tried using location blocks, but it gives me a "too many redirects" error when loading the site.
Thanks for any help you can give.
The if statement suggests that you are processing http and https requests in the same server block. You need to use two server blocks. This will remove the if block and allow you to use simple location blocks to handle the excluded directories.
For example:
server {
listen 80;
location /abcd/ {
...
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
...
}
Trying to do a simple redirect:
rewrite https://url.example.com(.*) https://example.com/plugins/url permanent;
Anytime url.example.com is hit, I want it to redirect to that specific path.
EDIT:
Will try to explain this better, as I'm trying to redirect to a specific domain from another.
server {
server_name example.com plugin.example.com;
root /home/www/example.com/public;
}
I see the location used for redirects such as:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
But not sure how to use it in my case, which is to change plugin.example.com to example.com/plugin.
For example:
http://plugin.example.com
https://plugin.example.com
https://plugin.example.com/blah
https://plugin.example.com/blah/more
All of these should redirect to:
https://example.com/plugin
If the original URL is https://url.example.com or https://url.example.com/ then the normalized URI used by the rewrite and location directives will be /. The scheme, host name and query string have all been removed.
To perform a permanent redirect to a URL with a different host name:
Using rewrite (see this document for details):
rewrite ^/$ https://example.com/foo permanent;
Or using location and return (see this document for details):
location = / {
return 301 https://example.com/foo;
}
The second solution is more efficient, as there are no regular expressions to process.
If the original URL includes a query string: The rewrite will append it automatically unless a trailing ? is added. The return will not, but can be added by appending $is_args$args.
If the scheme and host name are unchanged, then both statements can be simplified:
rewrite ^/$ /foo permanent;
Or:
location = / {
return 301 /foo;
}
Redirect from subdomain to subfolder on main site
Do you require a redirect from a subdomain to a subfolder on the main site?
This would be best accomplished by a separate server context, with the appropriate server_name specification.
Else, you could also do this with an if statement testing against $host.
As already pointed out elsewhere, rewrite directive operates based on $uri, which does not contain the hostname.
server_name-based matching (recommended):
Hardcoded redirect with a limited number of hostnames (recommended):
server {
server_name plugin.example.com;
return 301 $scheme://example.com/plugin$request_uri;
}
server {
server_name about.example.com;
return 301 $scheme://example.com/about$request_uri;
}
Regex-based redirect from any subdomain to the main domain:
server {
server_name ~^(?:www\.)?(?<subdomain>.*)\.example\.com$;
return 301 $scheme://example.com/$subdomain$request_uri;
}
Regex-based redirect from a limited number of subdomain to the main domain:
server {
server_name ~^(?:www\.)?(?<subdomain>plugin|about)\.example\.com$;
return 301 $scheme://example.com/$subdomain$request_uri;
}
if-based:
If-statement-based redirect with hardcoded hostnames:
server {
server_name .example.com;
…
if ($host = plugin.example.com) {
return 301 $scheme://example.com/plugin$request_uri;
}
if ($host = about.example.com) {
return 301 $scheme://example.com/about$request_uri;
}
…
}
If-statement-based redirect with a regex-based matching:
server {
server_name .example.com;
…
if ($host ~ ^(?:www\.)?(?<subdomain>plugin|about)\.example\.com$) {
return 301 $scheme://example.com/$subdomain$request_uri;
}
…
}
Please refer to http://nginx.org/r/server_name for more discussion of which option may be best for you.
You can create separate servers for example.com and plugin.example.com. And create redirect inside plugin.example.com server.
server {
server_name plugin.example.com;
return 301 https://example.com/plugin;
}
server {
server_name example.com;
root /home/www/example.com/public;
}
I have looked at this question which describes how to redirect a url in nginx, as shown below
# main server block for www.test.com
server {
listen 80;
server_name www.test.com;
...
}
# redirect test.com to www.test.com
server {
server_name test.com;
return 301 $scheme://www.test.com$request_uri;
}
What I need to do is redirect a set of individual pages, so was wondering how to do this
e.g. test.com\index , test.com\home , test.com\main to test.com\index.php
Then I have some other pages to simply redirect simply to the .php extension
e.g. test.com\about to \test.com\about.php
e.g. test.com\contact to \test.com\contact.php
What is the best way to do this?
Found the answer... assuming the following server block for test.com
server {
listen 80;
server_name www.test.com;
...
}
Add the appropriate regex location path, and rewrite or return to the redirect url.
for test.com\index , test.com\home , test.com\main to test.com\index.php
location ~ ^/(index|home|main) {
rewrite ^/.* http://$server_name/index.php permanent;
}
for test.com\about to \test.com\about.php
location /about {
rewrite ^/.* http://$server_name/about.php permanent;
}
I have been tasked with a couple project.
We have two directories on our server, one is
http://example.com/app
and the other is
http://example.com/fw
What I have been asked to do, is redirect from http to https if any visitor lands on a page in these two directories (app and fw)
Here is what I have done so far in the config file. When I added the lines to my config file below 'server' and restarted the site would not come back up. Unfortunately I don't have access to the log files. Appreciate anyone willing to take a look at this
location ~ ^/miner/memclub/.+\.html$ {
rewrite ^(.+)\.html$ /bootstrap.php?file=$1.html last;
rewrite ^(.+)\.phtml$ /bootstrap.php?file=$1.phtml last;
error_page 404 = /404.php;
}
server {
server_name site.org;
server_name *.site.org;
location /app {
if ( $scheme = http ) {
rewrite ^ https://site.org/app last;
}
}
}
First of all I don't think you can have 2 server_name, merge those two lines into one line
server_name example.com *.example.com;
And to do the https redirect i would recommend using 2 separate servers, you need one listening to port 443 anyway
server {
server_name example.com www.example.com; # which ever you are using
listen 443 ssl;
location / {
# all your https configuration
}
}
server {
server_name example.com www.example.com:
listen 80;
location /app {
return 301 https://$http_host$request_uri;
}
location /fw {
return 301 https://$http_host$request_uri;
}
location / {
# the rest of the non https configuration
}
}
I know you can merge both app and fw into one location, but I believe doing it without regex is faster, if you want to do it anyways here it is
location /(app|fw) {
return 301 https://$http_host$request_uri;
}
I have a site example.com that runs wordpress. Now I want to move this blog to subdomain blog.example.com, but also I want following:
example.com --> static page (not wordpress)
blog.example.com --> new address to the blog
blog.example.com/foo --> handled by wordpress
example.com/foo --> permanent redirect to blog.example.com/foo
So I tried this next config:
server {
server_name example.com;
location = / {
root /home/path/to/site;
}
location / {
rewrite ^(.+) http://blog.example.com$request_uri? permanent;
}
}
In this case redirection is working perfectly. Unfortunately example.com redirects to blog.example.com too.
The reason it's redirecting is because when it tries to load the index file of example.com, it performs an internal redirect to /index.html, which is handled by your rewriting location. To avoid this, you can use try_files:
server {
server_name example.com;
root /home/path/to/site;
location = / {
# Change /index.html to whatever your static filename is
try_files /index.html =404;
}
location / {
return 301 http://blog.example.com$request_uri;
}
}
As long as the root of the two domains will point to different directories, you'll need two server directives - something like this:
server {
# this is the static site
server_name example.com;
location = / {
root /home/path/to/static/page;
}
location /foo {
return 301 http://blog.example.com$request_uri;
}
}
server {
# this is the WP site
server_name blog.example.com;
location = / {
root /home/path/to/new_blog;
}
.... some other WP redirects .....
}