Creating a local custom host name instead of localhost? - dns

Currently, my flask app runs locally on:
http://localhost:5000/some_page
How could I create a local custom location for my app like:
http://myappname/some_page
Sort of like a local domain name. Is this possible at all? Any pointers would be great.

In order for the browser to resolve this custom name, you will need to add an alias to your /etc/hosts file. It probably already contains a line about 127.0.0.1, in which case you just add your alias to the list
127.0.0.1 localhost localhost.localdomain myappname
You can then change the server name in the app's config to make it explicitly use this name.
app.config['SERVER_NAME'] = 'myappname:5000'
Only privileged programs (run as root or with sudo) can bind to low ports such as 80, so you will still have to use a high port number.

It can be done using SERVER_NAME option in config:
app = Flask(__name__)
app.config['SERVER_NAME'] = 'myappname:80'
More information here:
http://flask.pocoo.org/docs/0.10/config/

Related

How do I set up my Linux Azure VM so that I can connect via a browser?

I'm working on a Linux VM on Azure which was set up by someone else (so I don't know all the details). I'm trying to connect it to a domain name.
The server has a "Hello World" program, so when I go to "example.com" I should be seeing "Hello World". Currently I'm just getting
Safari can't open the page "http://example.com" because Safari can't find the server "my domain.com"
I thought I'd start with making sure that the IP address connects to the server (which it did at one point. So I enter the IP address of the server (let's say it's "12.345.678.901") in the browser, and it can't connect... I get the error
Can't open the page "12.345.678.901" because the server where this page is located isn't responding
There's an Inbound port rule to allow connections for port 8080, so I tried "12.345.678.901:8080" but this time got
Can't open the page "12.345.678.901:8080" because Safari can't connect to the server
I don't know what to try next. Presumably something needs to be enabled on the server to allow the browser to connect?
The other inbound port rules are ssh on port 22 (TCP) and then what I assume are the standard Azure ones (I can't edit or delete them anyway).
To view your Linux VM inside the browser, you need to install a web server. Easiest to install and get working straight away is nginx.
First thing you need to do is SSH(port 22) into your VM using the username and IP address of the machine:
ssh username#ipaddress
Which will prompt you to enter a passphrase to gain access to the VM.
This also assumes your SSH public key exists inside ~/.ssh/authorized_keys on the VM. If you don't have this setup then you need to get the owner of the VM to copy your public key into this file. Otherwise you won't be able to connect and get a Permission denied (publickey) error.
Assuming the above works, you can install the nginx webserver with the following two commands:
sudo apt-get -y update
sudo apt-get -y install nginx
Then once this web server is installed, add an HTTP inbound port 80 rule inside the network settings. For security reasons, having your web server listen on this port is probably unsecure long term. Its just easier to get working when you choose this port to begin with, because its the default.
You can see what the default listening port by viewing the server configuration host file with cat /etc/nginx/sites-available/default:
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
Which shows the default port of 80. You can change this default port to 8080, then run sudo service nginx restart to restart the server and apply the changes. Additionally, you can have a look at this How to make Nginx Server Listen on Multiple Ports tutorial, which goes into more depth on how to configure listening ports for nginx webservers.
You should then be able to view your VM from a browser window(blurred out my IP address for security reasons):
You can also have a look at this Quickstart: Create a Linux virtual machine in the Azure portal tutorial for a step by step on how to get this setup in Azure.
You should first check to see if you have an entry for http://example.com. The reason could be that you do not have a DNS Entry and when you are trying to connect to it via the browser. Since you tried connecting to it via IP and it still did not work, I would suggest you check your Webserver configurations to make sure it is correctly listening for port 8080. Also, ensure that your webserver is also turned on as well. You can tail the webserver log and try to hit it via the IP like you did earlier and see if you see any errors in the logs. It would at least tell you if your request you are making on your browser is actually getting to the webserver.

Make my local nginx server private

Is it possible to make my local nginx server private from the wifi network I use. I've juste realized that anybody can access my local webapp with the public ip the wifi network give to me (typing it in the browser), and I dont want that...
Thanks
Inside of the configuration file that can be located at /etc/nginx/nginx.confon most systems. Please locate the line that starts with listen and change that line to listen 127.0.0.1:80; to only allow requests from the local interface and to drop all other requests. so that your friend will no longer be able to access your server after that be sure to save that file. Then restart your server as Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted. To reload configuration, run the following command:
nginx -s reload
Yes configure it to bind the loopback IP address 127.0.0.1: listen 127.0.0.1:80;

How to set up local subdomains for Node.js app

I am running an express app on node.js. The app uses the express-subdomain module to help handle routes for two different subdomains (sub1.example.com and sub2.example.com). I'm hosting the app on AWS Elastic Beanstalk. In my production environment, everything works great. But on my local machine, I cannot get this to work. I tried adding the subdomains to my host file 127.0.0.1 localhost sub1.localhost sub2.localhost. Although that allows me to prepend a subdomain to localhost, the module doesn't recognize this as a valid subdomain, and therefor searches for subdomain routes in my root routes.
In main.js:
var routes = require('./routes/index')(passport);
var sub1_routes = require('./routes/sub1')(passport);
var sub2_routes = require('./routes/sub2')(passport);
app.use(subdomain('sub1', sub1_routes));
app.use(subdomain('sub2', sub1_routes));
app.use('/', routes);
I need to be able to handle this locally. It takes to much time to push a small change to AWS test, iterate, etc.
I'm the author of the module :)
For each new subdomain you wish to test locally you must add into your /etc/hosts file. So for example:
localhost is:
127.0.0.1 localhost
a new subdomain would be..
127.0.0.1 sub1.localhost
and another..
127.0.0.1 sub2.localhost
Check out what I have done in the tests.
I had same exact problem and I found a simple solution. Instead of writing sub1.localhost try replacing localhost with lvh.me this is a domain that always resolves to localhost and now whenever you write sub1.lvh.me even though a port like sub1.lvh.me:3000 it will still work.
There is an awesome website, which someone hosted for all of us.
localtest.me
All requests will be routed to 127.0.0.1, including subdomains.
e.g something.localtest.me:3000
will resolve to 127.0.0.1:3000
but, for example, in your Express app, if you do
app.get('*', (req, res) => {
console.log(req.subdomains); // [ something ]
});
you'll get your subdomain
on Ubuntu
For creating subdomains for localhost you just need to follow 2 simple steps.
Open your terminal by pressing CTRL + ALT + T then run the following commands:
sudo vi hosts
sudo -i gedit /etc/hosts # to edit /etc/hosts file
Once you run 2nd command /etc/hosts file will open and now this is the place where you need to define subdomains.
Example: localhost is:
127.0.0.1 //our localhost
define new subdomain:
127.0.0.1 example.localhost # first
and another..
127.0.0.1 demo.localhost #second
that's it. Hope this was helpful.

Dokku - Add domain after setup

I installed Dokku on my Digital Ocean droplet, but did it before setting my dns records, so Dokku was installed on IP. Now I changed my dns record, so site can be accessed through site.com. I can access my previously created Dokku containers through site.com:port, how can I change Dokku settings to access my app like this - appname.site.com
Per https://github.com/progrium/dokku:
Set up a domain and a wildcard domain pointing to that host. Make sure
/home/dokku/VHOST is set to this domain. By default it's set to
whatever hostname the host has. This file is only created if the
hostname can be resolved by dig (dig +short $(hostname -f)). Otherwise
you have to create the file manually and set it to your preferred
domain. If this file still is not present when you push your app,
dokku will publish the app with a port number (i.e.
http://example.com:49154 - note the missing subdomain).
To fix the issue, you will first need to update the /home/dokku/VHOST file, adding the domain name -- this will fix any newly generated deployments, but existing apps will need to be deleted from the /home/dokku directory by name (/home/dokku/foo, /home/dokku/bar, etc.) and redeployed for this change to take effect, since each Dokku application has a separate nginx.conf within those /home/dokku/ paths and those will need to be re-written.
It is indeed not necessary to destroy and recreate apps. First, dokku domains:report tells you if global VHOSTS are already enabled or not. If not, run
dokku domains:add-global yourdomain.tld
echo yourdomain.tld | sudo tee -a /home/dokku/VHOST
dokku domains:add myapp myapp.yourdomain.tld
dokku domains:enable myapp
The first of these adds yourdomain.tld to /home/dokku/HOSTNAME. It should also add it to /home/dokku/VHOST, but it doesn't. So that needs to be done manually. Then tell dokku what (sub)domain you want to access myapp on. The last command sets the NO_VHOST variable for myapp to false.
To extend #shirkey answer: you don't need to re-create (destroy and create again) an app in order to apply those changes. You can manually create VHOST file inside /home/dokku/$APP/ directory (as dokku user) then remove NO_VHOST setting (dokku config:unset $app NO_VHOST) and change DOKKU_NGINX_PORT to 80 (dokku config:set $app DOKKU_NGINX_PORT=80) and restart the app (dokku ps:restart $app).
$ echo "example.com" > /home/dokku/VHOST
If you still could add a subdomain. These are the points to check.
Example: add myapp.example.com.
1, DNS(e.g. Namecheap)
If you are using Cloudflare, Check if Custom DNS is set to Cloudflare.
2, CDN(e.g. Cloudflare)
Check if it has A record like this.
Type | Name | Content
A | myapp | public ip address of Digital ocean server
3, VPS(e.g. Digital Ocean)
If you use Cloudflare you don’t have to set up Domain setting on Digital Ocean.
4, Dokku
Is port mapping set up properly? dokku proxy:report to check port 80
is mapped to the port of container.
Is the server running? Use curl from inside the server.
If you still could not locate the cause of the problem, check nginx config file like /home/dokku/appname/nginx.conf /etc/nginx/nginx.conf manually.
Example /home/dokku/appname/nginx.conf file
server {
listen [::]:80;
listen 80;
server_name myapp.example.com;
location / {
proxy_pass http://myapp-3030;
}
upstream myapp-3030 {
server 172.17.0.4:3030;
}

Assigning a domain name to localhost for development environment

I am building a website and would not like to reconfigure the website from pointing to http://127.0.0.1 to http://www.example.com. Furthermore, the certificate that I am using is of course made with the proper domain name of www.example.com but my test environment makes calls to 127.0.0.1 which makes the security not work properly.
What I currently want to do is configure my development environment to assign the domain name www.example.com to 127.0.0.1 so that all http://www.example.com/xyz is routed to http://127.0.0.1:8000/xyz and https://www.example.com/xyz is routed to https://127.0.0.1:8080/xyz.
I am not using Apache. I am currently using node.js as my web server and my development environment is in Mac OS X Lion.
If you edit your etc/hosts file you can assign an arbitrary host name to be set to 127.0.0.1.
Open up /etc/hosts in your favorite text editor and add this line:
127.0.0.1 www.example.com
Unsure of how to avoid specifying the port in the HTTP requests you make to example.com, but if you must avoid specifying that at the request level, you could run nodejs as root to make it listen on port 80.
Edit: After editing /etc/hosts, you may already have the DNS request for that domain cached. You can clear the cached entry by running this on the command line.
dscacheutil -flushcache

Resources