How to bind a domain name or virtual host to the Tornado Server
Tornado listens to IP addresses and ports by default. Due to the fact that Tornado is rarely used in China, Tornado provides more information. The following describes how Tornado binds a domain name.
By default, the hello word of Tornado is like this.
Copy codeThe Code is as follows:
Import tornado. ioloop
Import tornado. web
Class MainHandler (tornado. web. RequestHandler ):
Def get (self ):
Self. write ("Hello, world ")
Application = tornado. web. Application ([
(R "/", MainHandler ),
])
If _ name _ = "_ main __":
Application. listen (8888)
Tornado. ioloop. IOLoop. instance (). start ()
After running the command, use a browser to access the IP address + port 8888
If you want to support domain name access, you can use Nginx to listen to port 80 as a proxy, or if you only have such an application, simply use Tornado to listen to port 80 to avoid Nginx, the key is that others can access the website using IP addresses or domain names. Google has a lot of English sites and cannot find the tutorial for binding domain names. Anyway, the Tornado source file will be opened at that point. How can I write it inside? The result shows this passage.
Joy! I don't know why. This section does not exist in the Document. Then I will take a closer look at it. By default, the host mechanism is available for returning and any host access,
In this case, we can use the domain name binding method to run the tornado application, and support multiple domain names, because it is a string of regular expressions, add a domain name binding based on the example above.
Copy codeThe Code is as follows:
Import tornado. ioloop
Import tornado. web
Class MainHandler (tornado. web. RequestHandler ):
Def get (self ):
Self. write ("Hello, world ")
Class DomainHandler (tornado. web. RequestHandler ):
Def get (self ):
Self. write ("Hello, a.com ")
Application = tornado. web. Application ([
(R "/", MainHandler ),
])
Application. add_handlers (r "^ a \. com $ ",[
(R "/", DomainHandler ),
])
If _ name _ = "_ main __":
Application. listen (8888)
Tornado. ioloop. IOLoop. instance (). start ()
A.com access result
It can even support regular expression matching. For example, it can also support www prefixes.
Copy codeThe Code is as follows:
Application. add_handlers (r "^ (www \.)? A \. com $ ", [(r"/", DomainHandler),])
Execution result
With this mechanism, you can bind multiple domain names and sub-domain names to a single application for multi-site deployment. Easy to manage!
Why is your domain name not bound to the VM on this server?
To access a website normally, two conditions must be met:
1. Resolve the domain name to the IP address of the space.
2. the space is bound with a domain name.
Through your description, it should be that you did not do the second item, your domain name can access the server, but the server is not bound to a domain name, so I don't know which space to point to this server, so I see the prompts.
Solution:
In your space management platform, there is a "bind domain name", as long as you add a domain name to bind.
If you do not have the permission to operate the domain name management platform, contact your space service provider and ask them to help you. This operation is very simple. The service provider will be able to help you in two minutes.
How to bind a domain name to a VM
When you apply for a host, will you enter a domain name? If not entered, as follows:
Step 1: resolve and go to the domain name Management Panel and point the record to the IP address of the applied VM.
Step 2: apply for an ICP filing (Foreign hosts do not need to be used for backup). Contact the service provider of your application space and he will tell you how to do it.
Step 3: bind the host to the host Management Panel. You can add a domain name.
If you do not understand the above, you can use a very simple method to ask your virtual host service provider's customer service, to come to your IP address, and then contact your domain name service provider, send this IP address to him and ask him to resolve it to you. Then contact your VM service provider to bind it to you.