nginx or dns url rewrite or masking based on path - nginx

I work on one website which is travel blog and there is sub section mytravelsite.com/tickets which is whitelabel and everything under that mytravelsite.com/tickets/fares and other pages are pointing to another ip and this works fine.
But now i would like to switch and now main site to be mytravelsite.com which would be whitelabel from mytravelsite.com/tickets but without /tickets in url but still keep my blog so another trouble is that whitelabel don't have robots.txt so this i would have to serve too from my hosting.
So in the end i have hosting with 10.1.1.1 ip and whitelabel that resolve to 10.10.10.10 ip
What i need is:
everything under mytravelsite.com/blog to resolve to my hosting and my hosted website on 10.1.1.1
and also mytravelsite.com/robots.txt and mytravelsite.com/sitemap.xml to resolve to my hosting on 10.1.1.1
and everything else to resolve to the whitelabel at 10.10.10.10
i am guessing that this can be done either with some dns setup or nginx proxy or rewrite rules but everything i searched on internet for last 2 days ended up in failure.

DNS only considers the name, i.e. the part before the slash. You have always mytravelsite.com, so this can't be done purely in DNS.
I know this isn't a real/full answer.

I would try to solve it using proxy.
The included snippet is not a configuration of nginx but of an apache as I don't have similar setup for nginx within my reach now. But I believe it can be useful starting point though.
<VirtualHost 10.10.10.10:80>
ServerName mytravelsite.com
ProxyPreserveHost On
ProxyPass /blog/ http://10.1.1.1/ retry=1 timeout=600 keepalive=On
ProxyPassReverse /blog/ http://10.1.1.1/
ProxyPass /robots.txt http://10.1.1.1/ retry=1 timeout=600 keepalive=On
ProxyPassReverse /robots.txt http://10.1.1.1/
ProxyPass /sitemap.xml http://10.1.1.1/ retry=1 timeout=600 keepalive=On
ProxyPassReverse /sitemap.xml http://10.1.1.1/
</VirtualHost>
Notes:
10.*.*.* is a local-only network, so the 10.10.10.10 within the
virtualhost header is just for an illustration on what server this
definition should be put.
It also will not be working out of the box - just start here and
iterate to a final solution
You have not defined how could "your hosted website" be identified, therefore it is missing in the example

Related

Redirect http to htps on apache2 vhost behind aws ELB

I have a small problem with redirecting everything from http to https.
Here is the setup of my environment:
The server is a docker host which runs multiple docker containers. I'm trying to access a container that listens on port 9000. so I have a ProxyPass in the vhost file that looks like this:
<VirtualHost *:80>
ProxyPreserveHost On
ServerName subdomain.domain.com
ProxyPass / http://localhost:9000/
ProxyPassReverse / http://localhost:9000/
Redirect / https://subdomain.domain.com/
</VirtualHost>
The SSL Certificate is issued by AWS.
This server is behind a AWS Load Balancer. From the load balancer I have 2 target groups. One is redirecting 80 to 80, and the other 443 to 443.
Right now the https://subdomain.domain.com is throwing 502 bad gateway error.
Could someone have any idea what the issue here might be? Thanks in advance!
Does this help: https://www.namecheap.com/support/knowledgebase/article.aspx/9821/38/redirect-to-https-on-apache?
It seems like you need to redirect permanent.
I've tried adding this to the vhosts file:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{subdomain.domain.com/}%{REQUEST_URI} [L,R=permanent]
as indicated in the aws documentation. It doesn't work, I get an error saying invalid redirect. No idea what the problem is.
Anyone else experienced this problem?

Wordpress container behind Apache2 host can't serve static files

I have a Docker container with wordpress:latest in a host which has Apache 2.4 installed.
I added the lines below to my Apache configuration file, inside the vhost group:
ProxyPass http://localhost:8010
ProxyPassReverse http://localhost:8010
When I try to access my URL I can reach wordpress homepage, however all static files point to localhost so my layout doesn't work.
What am I missing? Some setup at Apache? Wordpress itself?
Apache modules are already enabled.
Edit 1:
Forgot to mention: this piece of configuration is inside a Location directive, which is inside a vhost directive.
<VirtualHost *:80>
...
<Location /usa>
RequestHeader set X-Is-Reverse=Proxy true
RequestHeader set X-Original-Host mysite.com.br
ProxyPass http://localhost:8010
ProxyPreserveHost On
ProxyPassReverse http://localhost:8010
</Location>
...
</VirtualHost>
Check that the Site URL setting in wordpress matches the URL your clients are calling.
This is the documentation on how to change the site URL in wordpress: https://codex.wordpress.org/Changing_The_Site_URL
If you proxy pass to your backend like that, requests coming into your container will be sent with the Host header set to localhost. Apparently, the WordPress container takes care of the host that has been set in order to generate static assets links. Try setting the following proxy option:
ProxyPreserveHost On
Just after the ProxyPass configuration line.
This options forward the Host HTTP header coming from the client over to the proxy connection. This way the backend will understand which public URL it's been called from and asset links should be correct.
Edit.
If you can't use the ProxyPreserveHost Directive you could try and directly set the Host header using:
RequestHeader set Host "your.host.name"

Apache reverse proxy and infinite loop

I have Server A and B. Server A hosts the main site www.example.com, Server B hosts another site using the subdomain test.example.com. On Server A I set up a reverse proxy, so that the Wordpress site running on test.example.com can be accessed via www.example.com/test
Now I want the subdomain test.example.com to be "invisible" for the user, so that a request to test.example.com will redirect to www.example.com/test.
Obviously, this got me into an infinite loop. I tried different things to break the loop to no avail.
Is there a way to do this?
<VirtualHost *:80>
ServerName test.example.com
RedirectMatch permanent ^/(.*) www.example.com/test
</VirtualHost>
Add the above apache conf and restart apache.

Apache Reverse Proxy to a Wordpress site passes subpath when it shouldn't

I've been trying to set up a reverse proxy on a main website to a blog site of the url format
example.com/blog -> blog.example.com
example.com/blog is on an Apache instance and in the httpd.conf I have added the following.
SSLProxyEngine on
ProxyPreserveHost Off
ProxyRequests Off
ProxyPassMatch /blog https://blog.example.com
ProxyPassReverse /blog https://blog.example.com
This all works but it keeps 404ing. The good news is it is actually reverse proxying correctly because it grabs the 404 page of the blog.
After looking at the apache access logs I found that it is passing the subpath for whatever reason /blog to blog.example.com so its fetching blog.example.com/blog. When users navigate to /blog, it does 404 naturally. However, my understanding was when setting up ProxyPassReverse is it would make the request at what was specified so in my above case it should be requesting blog.example.com and not passing the /blog at the end.
Here is the snippet from the documentation that confirms the above in how it should work:
For example, suppose the local server has address http://example.com/; then
ProxyPass /mirror/foo/ http://backend.example.com/
ProxyPassReverse /mirror/foo/ http://backend.example.com/
ProxyPassReverseCookieDomain backend.example.com public.example.com
ProxyPassReverseCookiePath / /mirror/foo/
will not only cause a local request for the http://example.com/mirror/foo/bar to be internally converted into a proxy request to http://backend.example.com/bar (the functionality which ProxyPass provides here).
Any ideas why this might be? Worst case I might try to add a redirect or a rewrite so /blog goes to the homepage but I do have my permalinks set up in such a way that the /blog is in the slug of articles.
FYI I am using Apache 2.2.
I'm an idiot. I was using ProxyPassMatch instead of ProxyPass. Ugh.

Wordpress returns IP address instead of domain

This was working for years until I did something that has now jacked this up....
Searching on this topic landed me in several places that basically said: You need to check the Wordpress URL and the site URL in the general settings in Wordpress. I have done so and they are both: http://www.petesworkshop.com/blog_wp . Yet, when I hit that URL I am redirected to my internal IP and get a error http://10.0.10.206/blog_wp (yep, that would be wrong!
Interestingly enough, I can get to the admin page just fine and it is just a subfolder of blog_wp. So, somehow things directed to /blog_wp return the IP but everything else is OK. Wierd.
Wordpress sits behind an Apache proxy which has this directive for the virtual host:
<VirtualHost *:80>
ServerName www.petesworkshop.com
ServerAlias petesworkshop.com
ProxyPass / http://10.0.10.206:5080/
ProxyPassReverse / http://10.0.10.206:5080/
</VirtualHost>
The server running the website behind the proxy has this:
DocumentRoot "/www/petes/htdocs/petesworkshop/"
ServerName www.petesworkshop.com
ServerAlias petesworkshop.com
DirectoryIndex index.html index.php index.htm
ErrorLog logs/petesworkshop.log
Options FollowSymLinks
Alias /blog_wp /www/petes/htdocs/petesworkshop/blog_wp
<Directory /www/petes/htdocs/petesworkshop/blog_wp >
Order Allow,Deny
Allow From all
</Directory>
<Directory /www/petes/htdocs/petesworkshop >
Order Allow,Deny
Allow From all
</Directory>
I am not sure how I jacked this up...it is probably something that I did to the Apache configuration but I can't be sure since everything else behind the proxy works fine. I even tried adding specific ProxyPass entries for /blog_wp but that didn't make any difference.
Stumped...
Check your wp-config.php for correct urls
like below
define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');
This one turned out to be an Apache proxy issue. What I needed was to add 'ProxyPreserveHost On' to the configuration, which I thought I had in there originally but I either removed it in an edit or had it placed in the wrong virtual host. In any case, adding the ProxyPreserveHost On flag, took care of the issue. Not sure why other entries in the same virtual host DID work without it but I am not going to worry about it.
I had a similar problem some time ago, whenever I wrote my domain in the browser it was solved correctly but my domain name was changed by the IP, after much research it turned out that the problem was in the hosting, not in the server, so which only change the configuration of my domain in GoDaddy since I was only doing a redirection but the correct thing was to create an A record that points to # (which represents your domain name) and everything started to work correctly. I hope it's help

Resources