Route a specific path to a specific EC2 instance using Route 53 - amazon-web-services

I'm not sure if this is even possible, but if so I'm looking for the best way to do it.
Say I want to host my blog for example.com on it's own EC2 instance, and I want the path to my blog to be example.com/blog
Is it possible to route all requests to example.com/blog/* to one instance, and all other requests to that domain elsewhere?
My web server is Apache.
Thanks!

You can now do this with Application Load Balancer and path-based routing: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/tutorial-application-load-balancer-cli.html#path-based-routing-aws-cli

Certainly it's possible, but not with DNS nor with an ELB. The most common solution to this is to use a web server that issues a 301 or 302 redirect.
In your case, example.com would point to whatever the main site is. The web server (nginx or Apache httpd, perhaps) hosting example.com would have a redirect for example.com/blog/* that is found at another destination.
Here's an SO post on using Nginx for a redirect and for using Apache for a redirect.

Yes, but you would have to proxy your requests through an instance handling example.com. How you configure this depends on your web server.
Some examples on how to configure this:
nginx: http://wiki.nginx.org/HttpProxyModule
Apache: http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

Since you are using Apache2 Server, so you can achieve this very easily by creating a Virtual Host.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/vchost1.com.conf
Create Virtual Configuration using the above command, it copies the complete code of content from the default file provided by the apache2 Server.
sudo nano /etc/apache2/sites-available/vchost1.com.conf
start configuring host & domain according to your requirements
<VirtualHost *:80>
ServerAdmin admin#domain.com
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
here also, you can multiple Virtual Host configurations in a single file, start configuring and enjoy hacking.
you can also multiple web applications in a single instance by using the same method and theis reference.
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-18-04-quickstart

Related

How to use Cloudflare's free SSL to force https on my Django application on Apache+mod_wsgi stack?

I wanted to use the free SSL (DNS) service provided by Cloudflare in my Django Application running on Apache + mod_wsgi stack. I can find setup examples for Nginx + uWSGI stack, but not for Apache + mod_wsgi stack so far. I was following this answer here https://stackoverflow.com/a/27650503/2051292 . But I couldn't convert the workaround from Nginx configuration to Apache/mod_wsgi. I'm facing few problems,
Even I use https to visit the web pages clicking on any of the internal links or submitting login/registration forms goes only to http pages.
(I actually want all the links to goto the https pages)
On the web page forms (login, registration) I'm getting CSRF Failed 403 Access Forbidden error. And I login at https page, but after verification the redirects to a http page with CSRF error.
Sometimes tweaking the configuration leads to redirect loop.
Would like to know the working configuration for using cloudflare free SSL with Apache + mod_wsgi stack and the Django settings also for the setup
EDIT: In case if you don't know, this is how Cloudflare's free SSL works.
I don't have an SSL certificate with me, I'm trying to use the free SSL by using the Flexible SSL. i.e. The connection from user to Cloudflare DNS will be https and the connection between Cloudflare and my server will be http.
Note: Currently I'm NOT using any middleware like django-sslify . But using it will help any other way. I'm okay with that also.
While I don't know the specifics of using Cloudflare, it should be the same Apache configuration once you have the appropriate certificates nave figured out the proxy settings mentioned in the other answer. Here's an example Apache configuration I have used for hosting Django with mod_wsgi and mod_ssl under CentOS. Under this configuration, we don't even listen to port 80. This assumes your web site is hosted at https://example.com/mysite, and that it is deployed to /var/www/html/mysite under the user mysiteuser with a virtualenv called mysitevenv:
LoadModule wsgi_module modules/mod_wsgi.so
LoadModule ssl_module modules/mod_ssl.so
WSGISocketPrefix /var/run/wsgi
NameVirtualHost *:443
Listen 443
<VirtualHost *:443>
ServerName example.com
ErrorLog /home/mysiteuser/apache_errors.log
WSGIDaemonProcess mysite-https python-home=/home/mysiteuser/.virtualenvs/mysitevenv
WSGIScriptAlias /mysite /var/www/html/mysite/mysite/wsgi.py process-group=mysite-https application-group=mysite-https
WSGIProcessGroup so-https
Alias /mysite/static/ /var/www/html/mysite/static/
SSLENGINE on
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
SSLProtocol all -SSLv2
</VirtualHost>

setup sub domain amazon using Route 53

im really confused regarding setting up sub domain on amazon using their DNS service (hosted zones) and another 3rd party registrar. This is because i found different solutions some include add CNAME record and others say add using virtual host in the apache server
so should i do both or adding the CNAME in the records is enough.
so far, I have tried the first solution
I have added CNAME record (for example, let s assume my website is example.com)
so I have added CNAME subdomain.example.com pointing to example.com/subdomain. I have waited for 1 day so the DNS settings to be propagated, still when I check the website i get an error.
i've also tried to add virtual host in my conf file, but it didn't work at all.
I think the answer is very clear in the referenced link, but to clarify more what you should do , Follow the following steps:
1 - Go to your AWS managment console -> Route 53.
According to your question, it looks that you are already using AWS DNS service, which means you have already created hosted zone (in case you don't have hosted zone create one )
For more info, follow the documentation http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/creating-migrating.html
2 - Click on the hosted zone, then click on create record set
the record set should be type A pointing to your #IP of your server (Elastic IP or Load balancer IP)
Example:
subdomain.example.com -> Type A -> #IP
By the way you can use CNAME record for www.subdomain.example.com that points to subdomain.example.com
3 - the last step is to add Virtual Host in your configuration file of your appache server.
open this file /etc/httpd/conf/httpd.conf.d
Add the following Virtual Hosts
<VirtualHost *:80>
ServerAdmin webmaster#yourdomain.com
DocumentRoot "/var/www/html/domain_folder"
ServerName example.com
ErrorLog "logs/example.com-error_log"
CustomLog "logs/example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#yourdomain.com
DocumentRoot "/var/www/html/subdomain_folder"
ServerName subdomain.example.com
ErrorLog "logs/subdomain.example.com-error_log"
CustomLog "logs/subdomain.example.com-access_log" common
</VirtualHost>
4 - Restart httpd by typing this at the command line:
> sudo service httpd restart
PS :
=> if this doesn't work, here is one possible cause. There might be another configuration file interfering with this one. Check for another .conf file in /etc/httpd/conf.d. Often your primary httpd.conf will include another .conf file in this directory. Make sure it doesn’t have some Virtual Host settings which are interfering with yours. If so, comment them out.
=> also make sure NameVirtualHost *.80 is uncomment

django apps on nginx running separately from apache

I am installing production server for my django apps and I can not make it work. My configuration files can be found here.
Basically I have apache2 installed and running on port 80 for my php applications. I want to run my django apps on nginx with uwsgi, apart from apache2. So I am running nginx on port 8000.
When I open http://IP:8000/ I can see my site properly.
1. But how do I set it up with domain name?
I've set A tag in dns to IP. Now it hits apache2 "it works" page because it hits port 80 on default? So I need to proxy pass all requests to my domain.com? Something like this?
/etc/apache2/sites-enabled/domain.com:
<VirtualHost *:80>
ServerName domain.com
ProxyPreserveHost On
ProxyPass / http://IP:8000
</VirtualHost>
It does not work, so how do I pass all domain requests from apache to nginx?
2. How do I add another domain name? (new app)
Do I just create new socket file for new app, keep it on port 8000 and nginx will decide depending on domain name what conf file to use?
I have not found any similar tutorials, nginx usually handles static files and sends requests to apache2. However I want it other way.
Thanks for your answer. To make it work I had to set apache proxy like this:
<VirtualHost *:80>
ServerName www.domain.com
ProxyPreserveHost On
ProxyPass /static http://XX.XX.XX.XX:8000/static
ProxyPassReverse /static http://XX.XX.XX.XX:8000/static
ProxyPass / http://XX.XX.XX.XX:8000
ProxyPassReverse / http://XX.XX.XX.XX:8000
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.(?!\.css|js|gif|png|jpg|ico))*$
RewriteRule /(.*) http://XX.XX.XX.XX:8000/$1 [P,L]
</VirtualHost>
and enable proxy_http:
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo service apache2 restart
1. How do you set it up with domain name?
In the server block of your nginx conf file set the server_name:
server {
listen 8000;
server_name www.my-django-domain-one.foobar;
#rest of your config regarding forwarding to django...
}
Your site will be available at http://www.my-django-domain-one.foobar:8000.
2. How do you add another domain name? (new app)
Nginx will not decide anything based on the conf filename. Create a new conf file or use the existing one (only matters in the sense of how you want to organize your configs)
server {
listen 8000;
server_name www.my-django-domain-two.foobar;
#rest of your config regarding forwarding to django...
}
However, I recommend an approach involving only one web server. Opinions on which to use vary of course but both of them can do what you want to achieve on their own. You add unnecessary complexity to your setup (e.g. two servers to keep patched) and -depending on your traffic- it may even have a significant impact on your performance.
Check out this tutorial to see how you could make your php apps work with nginx and php-fpm.

multiple domains, multiple django projects, one server

Hi I'm new to apache and mod_wsgi and am trying to figure out how to configure both so that i can have www.example1.com use djangoproject1 and www.example2.com use djangoproject2.
I followed this tutorial to hook up example1.com to djangoproject1 and it works beautifully, but the tutorial doesn't exactly give the most detailed explanations for what is going on and why i need to do certain things.
what i have so far:
1) a dns zone for example2.com pointing to the server ip
2) installed python environment and django and started a new django project for djangoproject2 per the instuctions on the tutorial
I'm pretty sure i'm going to have to create a new wsgi config file and add a site configuration in /etc/apache2/sites-available/ called example2.com and then enable it, but i'm not sure about what else i'd need to do.
You can have 2 virtual hosts in your apache configuration.
The first virtual host on (default) - port 80
second one on port 81
In the virtual host, you can specify it this way:
NameVirtualHost *:80
<VirtualHost *.80>
DocumentRoot django_project_1_path
.... other config
WSGIScriptAlias / path_to_wsgi_config_1
</VirtualHost>
NameVirtualHost *:81
<VirtualHost *.81>
DocumentRoot django_project_2_path
.... other config
WSGIScriptAlias / path_to_wsgi_config_2
</VirtualHost>

HOw can i use subdomain with django and mod_wsgi

I have my main site at www.example.com
I want to run sites like site1.example.com
I tried this
<VirtualHost ip:80>
DocumentRoot /home/user/django/app
ServerAlias site1.example.com
I have also added record to /etc/hosts but its not working.
i am able to ping site1.example.com from server but not from outside
i am able to ping site1.example.com from server but not from outside
This is not a Django problem. ping is a low-level IP protocol (ICMP) that is handled well down from the HTTP server (application) level.
Adding something to /etc/hosts only affects the machine that /etc/hosts is on.
You should get subdomains working with static web pages (e.g. "Hello World") first, leaving Django out of it. Then read up on django.contrib.sites and go from there.