Nginx URL masking to another domain - nginx

I just installed Nginx as a feature of VestaCP on my VPS.
What I'm trying to do is to mask the URL http://example.com to http://12345.com, so when users visit http://example.com/path/file.mp4 they will see the content of http://12345.com/path/file.mp4 but browsers still show the URL http://example.com/path/file.mp4.
I googled and found a topic here. It looks like the answer I'm looking for. However when I applied his code to nginx.conf, VestaCP showed Error: nginx failed to start with new config and stopped working.
Here is the code:
server {
listen 80;
server_name sub.example.com;
location / {
proxy_pass https://123.12.12.12;
rewrite ^/$ /path last;
}
}
Is this the right solution for me? I'm completely new to this so I don't know if I did it correctly.
Edited: I was able to get the job done using apache .htaccess. How do I convert this to use with Nginx?
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^ http://12345.com%{REQUEST_URI} [L,NE,P]

You just need below, no rewrite needed
server {
listen 80;
server_name sub.example.com;
location / {
proxy_pass https://123.12.12.12;
}
}

Related

WordPress multisite home redirection issue using SSL

I am working on a WordPress network site and added SSL. Now my home page has redirection issue. Rest of the pages works properly. For now, I am not able to login to the admin panel.
I have used permalink as "postname". Inside the database, I have changed the site and home URL to "https://mysiteurl.com".
Please suggest the required changes I need to place to redirect my home properly.
Please add this code to your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
then add this line of code at the end of your wp-config.php:
define('FORCE_SSL_ADMIN', true);
UPDATE
If you use nginx include this in your configuration file:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}

Nginx redirect top level domain

I've just started using Nginx instead of apache.
I'm trying to find a similar way to do something like this, in Apache:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
But in Nginx.
So basically any .co.uk traffic gets redirected to .com
You could create a separate server block for the .co.uk domain:
server {
listen 80;
server_name .example.co.uk;
return 301 $scheme://www.example.com$request_uri;
}
See this document for details. Note that the default_server option can also be used to match any non-specific domain name.

How to convert this apache rewrite rule to nginx

I've been struggling to convert my past apache rewrite rule to nginx (also not sure if I'm placing it in the right place so would appreciate if you can tell me where to place it).
Basically this was apache rewrite rule on my .htaccess file on Wordpress:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?hosts/(.*)$ /user/$1 [R,L]
</IfModule>
As you can see, I'm using this rule in order redirect from example.com/hosts/username to example.com/user/username
I've used this converting tool https://labs.gidix.de/nginx/ and it outputs this conversion: rewrite ^/?hosts/(.*)$ /user/$1 last; - however I tried placing this in Ajenti's(control panel) advanced custom configuration but it's not working.
as an option
location ~ ^/hosts/(.*)$ {
return 301 $scheme://$host/user/$1;
}
You probably need to make this an external redirect so that WordPress will take notice. The cleanest solution would be to place it into a location of its own:
location ^~ /hosts/ {
rewrite ^/hosts(.*)$ /user$1 permanent;
}
See this document for more.

Nginx: I cannot get a 301 redirect from the Query Paramaeter to the Path

I would like to redirect:
From:
http://www.fascinate.jp/index.php?option=com_virtuemart&page=shop.browse&manufacturer_id=4&Itemid=62&limitstart=0&lang=en
To:
https://www.fascinate.jp/english/brand/devoa/
The following is the redirect settings for my current site:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{QUERY_STRING} (^|&)option=com_virtuemart&page=shop\.browse&manufacturer_id=4&Itemid=62&limitstart=0&lang=en($|&)
RewriteRule ^index\.php$ https://www.fascinate.jp/english/brand/devoa/? [L,R=301]
How should I rewrite the Apache Settings so I can use it with Nginx?
The version of Nginx I am using is nginx/1.9.11.
The second rewrite condition translates directly to an if block. The actual rewrite translates to a return 301 in its simplest form. Like this:
if ($args ~ "(^|&)option=com_virtuemart&page=shop\.browse&manufacturer_id=4&Itemid=62&limitstart=0&lang=en($|&)") {
return 301 https://www.fascinate.jp/english/brand/devoa/;
}
nginx is not good at implementing multiple conditions. The RewriteCond %{SERVER_PORT} 80 will only cause problems if you serve both SSL and non-SSL from the same server block. If you can separate out non-SSL into its own server block, then the above if block can be applied only to the non-SSL URLs.
See this and this for more. And of course this.

How to rewrite based on accept heads in nginx

I recently switched over to nginx and am fairly new at it, so forgive me if this has been covered extensivly before.
What im trying to do is rewrite the user request based on tthe accept header sent out.
In paticular:
if the accpet header is an image/gif or image/webp then serve the image, if not concat off the .gif of the url and serve that.
hears my apache configuration, but again im on nginx now and trying to learn how i could convert it over:
RewriteCond %{HTTP_ACCEPT} ^image/gif [NC]
RewriteCond %{HTTP_ACCEPT} ^image/webp [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com(.*)$ [NC]
RewriteRule ^i/(.*)\.gif http://example.com/i/$1 [R=302,L]
as you can see the above htaccess file works like a charm, but nginx is completly different it seems.
Ive done some reading and come up with this:
map $http_accept $webp_suffix {
default "";
"~*webp" ".webp";
}
with the following inside the server block
location ~* ^/i/.+\.(gif)$ {
root /storage-pool/example.com/public;
add_header Vary Accept;
try_files $uri$webp_suffix $uri =404;
}
Sadly, this doesnt work and I still dont know how to trouble shoot in nginx either.
Any information would be greatly appreciated, thanks.
From your location, the try_files will concat $uri with $webp_suffix which might not what you want, for example if you have a request to /i/test.gif with HTTP_ACCEPT header set to image/webp your location config above will try to find files at:
/storage-pool/example.com/public/i/test.gif.webp
You probably want something like this below instead:
location ~* ^(?P<basename>/i/.+)\.gif$ {
root /storage-pool/example.com/public;
add_header Vary Accept;
try_files $basename$webp_suffix $uri =404;
}
This location will capture the image path without the .gif suffix and it will be available as variable named $basename
More information about named capture here: http://nginx.org/en/docs/http/server_names.html#regex_names

Resources