How can I host my website's root url ('/') on one hosting provider and every other url on another? - nginx

So I have a domain(example.com), where I want the root url
example.com
to direct to my landing page's DNS that comes from its hosting provider and I want every other url
example.com/whatever-here
to direct to my main hosting provider, which is digital ocean with its DNS. I did it before, but with subdomains. The example.com part was directing to my landings and their hosts, while everything that started with the "app" subdomain (app.example.com) directed users to my main hosting. Now I want to do it without introduction of subdomains. Is it done somewhere on Nginx level? Or is it configured in my hosting providers' settings? Couldn't find the answer so far.

You can't.
The hostname indicates what server your browser is going to connect to. The requested path is then sent to that server, only after the browser has connected to it.

Just as #Brad said, it's impossible to directly host a domain name on two different DNS'es simultaneously. However, I managed to find two approaches that don't do what I wanted, but the end result is pretty much the same:
Host www.domain.com on one server and domain.com on another. Not all providers support it, so it's a not guaranteed-to-work solution. But it's still a solution.
Use Nginx's proxy-pass without redirect or a similar function of your webserver.
This way, you can have example.com actually load some-other-site.com without changing the browser url. I'll provide my Nginx configuration for reference:
location = / {
include proxy_params;
proxy_pass http://other-domain-or-ip.com/;
}
This is just the result I wanted and it makes me think that I fell victim to the X->Y problem here.

Related

How to set up a Google VM (instance grouped), https load balanced w/CDN, so that the backend resolves the domain?

Backend: "Bitnami WordPress with NGINX and SSL Stack for Google Cloud Platform" from marketplace.
I used this guide: https://www.am22tech.com/google-cloud-cdn-wordpress/
I ended up with a somewhat working system as follows:
My Domain ->(google managed cert)-> CDN + Load Balancer -> Instance Group ->(http)-> VM with bitnami stack.
All works well and seems very fast. The biggest gap in my understanding is how the VM can be told it needs to behave as if it's the original domain.
For example, in the nginx server config, any kind of reference to $host seems to return the VM's IP address or something like that.
Also, in wordpress, in a lot of places the domain is replaced by an IP address, even though the site URL and wordpress address show up correctly. This isn't a wordpress question though, as I'm quite sure there is a more general solution I'm missing to do perhaps with NGINX or the load balancer configuration. I think PHP detects the host and passed it along to wordpress but I'm not clear how.
I found a reference somewhere in the google documentation how to manually assign a domain to a VM but not sure that's what's needed here.
Further to this, I'm totally unclear how I would set up https between the vm and the load balancer, yet only have one domain/ip address for the global forward rule. Maybe a separate question.
Bitnami Engineer here. If you already have the domain, certificates and the Load Balancer in place, you will need to configure WordPress to use that domain name as default domain of the application. You will need to edit the wp-config.php file and configure these lines
define('WP_SITEURL', 'http://DOMAIN/');
define('WP_HOME', 'http://DOMAIN/');
More info: https://docs.bitnami.com/google/apps/wordpress-pro/administration/configure-domain/
In case you also want NGINX to redirect you your domain, no matter how you access your app's information, you can add this configuration line
return 301 https://DOMAIN$request_uri;
in the /opt/bitnami/nginx/conf/bitnami/bitnami.conf file
More info: https://docs.bitnami.com/google/apps/wordpress-pro/administration/force-https-nginx/
I had better luck having the load balancer talk to my VM with https. Once I got that working, I didn't have to make any changes to wp-config.php. In this case I didn't bother with varnish because I think it only supports http. I'm hoping google's CDN will be sufficient regarding caching, and I may try a helper plugin in wordpress.
To redirect http to https, I followed the bitnami instructions to set up to front ends to the load balancer pointing to the same static ip address, then in my nginx server blocks, I added a redirect line in the https block (not the http block), since the google load balancer communicates with my backend via https. Google sets the http_x_fowward_proto to http so I check that and redirect if necessary.
if ($http_x_forwarded_proto = "http") { return 301 https://$host$request_uri; }
The bitnami stack is amazing, everything seems extremely fast!

How can I have a AWS subdomain website point to another EC2 instance which hosts wordpress?

Here is the situation:
I have a static website hosted by AWS S3(www.mysite.com), however, I want to also attach a blog to a sub-path with in my domain (www.mysite.com/blog) which uses wordpress on an EC2 instance.
How would I go about it?
Yes, this can be done, but you need to understand why the solution works the way it does.
A domain name points to a single logical endpoint, which handles all requests for the domain. If certain paths are handled by one system and other paths are handled by another one, then a single system must be in charge of routing those requests, and routing them through itself.
You cannot configure how paths are handled using DNS.
In my answer to Can I use CloudFront to serve a WordPress blog from the same domain, but a different server? (at Server Fault), I gave an overview of how this can be done in an AWS-centric solution. CloudFront, in addition to providing caching, is a reverse proxy, and that's fundamentally what you need, here: a reverse proxy to examine each request and select the correct back-end server. There are other solutions, such as using HAProxy in EC2 to handle request routing but they are more complex and may not perform as well in all cases.
You configure a CloudFront distribution with two origin servers (your bucket's web site endpoint and the Wordpress server), and use two cache behaviors so that /blog* goes to Wordpress and everything else goes to the bucket. Then you configure your domain name as an alternate domain name on the CloudFront distribution, and point your domain name to CloudFront in DNS.
The slightly tricky part here is that the wordpress site must be rooted at /blog and not at / because CloudFront will actually send the /blog (at the beginning of the path) to the WP machine, so it needs to expect that. CloudFront uses the path prefix to select the origin server (by finding the matching cache behavior for that path), but it can't remove the path prefix.¹
Wordpress is not my specialty, the this appears to require some WP configuration changes that appear to be fairly straightforward.
¹ CloudFront can't remove the path prefix. This will change when Lambda#Edge launches, which is an integration of CloudFront and Lambda functions, allowing server-side examination and manipulation of request and response headers from within the CloudFront infrastructure, but even when that is a available, it will still be advisable to configure WP to not be at the root, for simplicity.
By a different subdomain, I'm assuming that you want the blog to be at blog.mysite.com. I think that is the only way to go (you cannot use a /blog but will have to use a subdomain). In that case, following steps should work:
Create an Elastic IP and attach it to the EC2 instance
Configure the EC2 WP instance to respond to blog.mysite.com
In the DNS provider of www.mysite.com, point the A record of blog to point to the Elastic IP of the EC2 instance

Alternative to defining aliases in /etc/hosts?

Is it possible, without editing the /etc/hosts file, tell my computer redirect to 127.0.0.1 every time I visit domain1.com or domain1.com through the web browser as well as when I request the content of the same pages through curl?
Run a DNS server/resolver on your machine, configure to forward every query that it can't resolve to the DNS resolvers upstream and set /etc/resolv.conf to direct all queries to the locally running resolver.
Then in the local resolver add entries for the domains you want to blackhole toward localhost.
There are several of options to chose from. The currently most popular caching resolver is unbound, but you can also use dnscache for it.

How do I give access to another computer on my network, to my website hosted locally?

We have a local instance of IIS 7 running with a website. Instead of the default "localhost" we have something like, mysite.compname.com. This is a separate entry into IIS 7 and the default website was removed to prevent confusion.
Then in our host file we an entry like this:
127.0.0.1 mysite.compname.com
Now when I try to hit this url, http://127.0.0.1/ApplicationName/Project/AddProject.aspx technically it should work, but instead I get a 404. I can vouch that this isn't a problem with the application, because if I navigate to http://mysite.compname.com/ApplicationName/Project/AddProject.aspx it works fine.
My end goal is to be able to give someone my computer name, so that they can visit a test page, so the url above I think would get turned into this http://computername/ApplicationName/Project/AddProject.aspx. Any help or at least links to understanding would help because I'm not sure where my issue is coming from.
It sounds like the IIS site / application is configured using a Host Header.
This means that the site will only respond if the host header sent by the browser matches the one configured for the site.
This is a standard method to allow one server to host sites for many host and domain names.
If you wish to allow others to view the site on your computer you will need to either have a local DNS server which you can edit, or, probably the easiest option, get them to edit their host files to include
<your IP> mysite.compname.com.
Remember to open the requisite ports (probably only 80, maybe 443 for https) in your firewall.
Or, you can try to edit the site config to remove or modify the Host Header requirement. See the first link for details, but be careful, it's easy to break things if you don't know the entire architecture of the site.

Create subdomain - IIS7

Maybe some one can help me understand why i can't create sub domain. My site is working fine for
mydomain.com and www.mydomain.com
but when i added staging.mydomain.com to the bindings i can't reach to that address for some reason. i read a post here that someone mention the app.config handler setting but i don't know what.
thanks.
What is happening is that your hosting provider probably has set host names for your site for www.mydomain.com and domain.com, however you would need to ask him to add staging.mydomain.com to the list of host names.
unfortunately IIS does not support wildcard host headers, so you will need to specify the actual host names that you need (that is unless you own the IP address at which point you could just use * which will allow any Host name to be seen there and you could use URL Rewrite to do cool stuff, but owning the IP is rarely the case).

Resources