Hosts file working for custom subdomains but not custom domains - django

I am developing a multi tenant app and need to modify my etc/hosts file to test the different URLs tenants might use. Some will use a sub domain like tenant1.mysite.com and others will use entirely their own URL like tenant2.com (to mask the fact it's a whitelabel site and pretend its their own).
In my hosts file I have:
127.0.0.1 mytenantsdomain.com
127.0.0.1 localhost
127.0.0.1 thor.localhost
127.0.0.1 potter.localhost
127.0.0.1 testtenant.com
localhost, thor.localhost, potter.localhost all work as expected when adding :8000 to them. i.e. they connect to the local development server and show expected tenant-specific content. But, mytenantsdomain.com and testtenant.com both give ERR_CONNECTION_REFUSED - I'm guessing its for the lack of the port :8000 tbh.
I have tried a few fixes like flushing the cache with the below but nothing has worked.
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Anybody know what else I can try to get them all working?

For anyone coming to this later - after a bit more digging (on serverfault.com), the following lessons:
etc/hosts isn't the place for this. It just resolves a text domain
name to an IP. It's nothing to do with ports.
The solution was just to add :8000 after the .com - http://mytenantsdomain.com:8000/ resolves to the local dev server.
To make this permanent (i.e. not have to add :8000 to it), port forwarding would be the way to go. So you need a solution something like this: https://serverfault.com/questions/791181/redirecting-traffic-to-a-specific-address-and-port-using-pf-on-macos

Related

Connect local domain to Django

I have several applications that I'm running on Docker. Each one of them has own domain, however when I start containers I can access them only on 127.0.0.1:8000 or 127.0.0.1:8001. But I need to reach them on domain like test1.mydomain.local and test2.mydomain.local.
I tried to change my host file like this:
127.0.0.1 *.mydomain.local
127.0.0.1 localhost
However when I start docker again - it doesn't work. I have .env file where all domains are written down but I don't get how to get this worked.
Please help me to figure it out.
It's not possible to specify wildcards in the host file. Set them explicitly or use a local DNS.

Plausible analytics on a server with a webapp

I have Django hosted with Nginx on DigitalOcean. Now I want to install Plausible Analytics. How do I do this? How do I change the Nginx config to get to the Plausible dashboard with mydomain/plausible for example?
Setup plausible by either running the software directly or in a docker container - let's say it runs on port 8080
Then in your nginx.conf - you should have a server block for your domain
Within that add a location block with the path you want plausible on and add a proxy pass directive to forward the requests to localhost:8080
Monitor access.log and error.log to debug any issues that may happen

Setting up a Django server with Gunicorn and Nginx

I've followed this tutorial and i'm running into an ERR_SSL_PROTOCOL_ERROR that i just can't figure out.
I must mention that i have set inside settings.py:
DEBUG = FAlSE
SECURE_SSL_REDIRECT = False
And my Allowed_hosts is ['*'].
I have a number of questions which reading alot of blog posts hasn't answered:
In the nginx configuration, if i'm running the server from a laptop connected to a router, and the external IP is 12.34.56.78 and the port is 50000, what am i supposed to put at server_name?
In the gunicorn configuration, everyone states that this is the config command: gunicorn --bind 0.0.0.0:8800 AWESOME.wsgi:application what IP should i put there in my case? 12.34.56.78:50000 or just leave it like it is?
As i understand the situation, given the setting in django settings.py my server is not serving HTTPS so the error does not come from that. I've also read about certbot but since i don't have even those 2 above questions figured out i cannot understand how to configure certbot..
EDIT
In fact i've tried to:
gunicorn --bind 12.34.56.78:50000 KYng.wsgi:application but i'm getting invalid address error
1.) You actually don't need to put anything in there, as it will default to an empty string.
2.) You should put localhost (127.0.0.1) if you want it to run locally, or 0.0.0.0 if you want it to run publicly.
Concerning the ssl error, check your nginx configuration more closely...that you didn't reference https instead of http somewhere...

How can I change django runserver url?

I'm trying to change django project url, so that users, who wants to connect to website in local area network will see url instead of localhost:8000 or 127.0.0.1. I need to change localhost:8000/users/board to be http://example.eu. I've tried to python manage.py runserver http://example.euand then thought about changing url's but it doesn't work. Also tried to change hosts file(Windows OS), but as far as I understand I need real ip address not url. Is it possible to do this thing and how?
You can use python manage.py runserver 0.0.0.0:8000. 0.0.0.0 means all IPv4 addresses on the local machine. So the server can be reachable by 127.0.0.1 and your private ip address like 10.10.5.8. So now others can access the server using http://10.10.5.8:8000. You the runserver on port 80, so that port can be removed from the url (by default is 80).
But to use any domain instead of ip, you have to change the hosts file of all the clients using the server to add domain to ip address mapping. Alternatively you can configure local network server to map the particular url to your system ip.
Run a local domain with the same port
Opening the /etc/hosts file on your mac with
sudo nano /etc/hosts
And for windows I believe you need to open:
C:\Windows\System32\Drivers\etc\hosts
In here add the domains you want, for example I added vazkir.com
....
127.0.0.1 localhost
127.0.0.1 vazkirtje.com
127.0.0.1 www.vazkirtje.com
.....
Lastly you can add it the domain to you ALLOW_HOSTS in your settings.py:
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "vazkirtje.com", "www.vazkirtje.com"]
And now you can visit your Django application (with port 8000) at:
http://vazkirtje.com:8000
The solution is not perfect since you do have to specify the port you are using, but this does let you use a domain for your local Django application in a relatively easy manner;)
Run a local non-existent domain without specifying the port
If you do want to run it without specifying the port; so just vazkirtje.com then you can use port 80, because this is the default port for HTTP.
Make sure the domain you are testing does not exist, since the domain lookup will first be done on existing domains. So check if you get a similar message to the one I got on chrome, when visiting the url:
Now you can specify this port by adding port "80" to the "runserver" command. You only do need to use "sudo" to run the command at this port, since you need admin rights for this. So run:
sudo python manage.py runserver 80
And now you should be able to access your Django application by visiting:
http://vazkirtje.com

How to locally test Django project accessing it using example.com domain?

I'm willing to test Django project, but I want to access it using “example.com“. Before I used “127.0.0.1:8000“ (manage.py runserver), but now I have need to test how my project acts on different TLDs. All I want to do is somehow tell my computer that “example.com“ points to “127.0.0.1:8000“. I don't want others to have access to it, so it's not question about deployment.
I am Linux user. Any help would be much appreciated!
sudo vi /etc/hosts and add the following line at the end.
127.0.0.1 example.com
Also, if you run the web server as root, you can use port 80 and not have to type example.com:8000 into the address bar.
change the hosts file at /etc/hosts
Just edit /etc/hosts so that example.com maps to that 127.0.0.1.
I don't think you can map it to a specific port number though, so you'll need to either access example.com:8000 or run the server on port 80 (probably via sudo).
Simple, edit your hosts file at /etc/hosts so that your site "example.com" maps to the localhost "127.0.0.1"