Simple routing question

250 views
Skip to first unread message

Jim S

unread,
Feb 11, 2013, 6:36:30 PM2/11/13
to web...@googlegroups.com
I'm trying to route traffic that comes in on a specific URL to a specifc app.

Example:

www.host1.com should route to the welcome app

www.host2.com should route to mySpecific app

I realize this is probably trivial, but I'm really struggling with it.  Hoping to do it with routes.py and not through wsgi stuff.  Please feel free to set me straight if that is not advisable.

-Jim

Jonathan Lundell

unread,
Feb 11, 2013, 7:32:41 PM2/11/13
to web...@googlegroups.com
Look at the domain-routing provision in the parametric router. Documentation in the book, and in router.example.py.

Jim S

unread,
Feb 11, 2013, 10:01:41 PM2/11/13
to web...@googlegroups.com
Jonathan

I am currently using that as my base for getting this working.  Here is what I have so far:

routers = dict(
    # base router
    BASE=dict(domains = {"www.website1.com":"mustangs",
                    "www.website2.com":"icysa", }))

But, anytime I to either URL, I get the web2py welcome app.

Also, I've saved the file as routes.py.

-Jim

Jonathan Lundell

unread,
Feb 11, 2013, 10:06:35 PM2/11/13
to web...@googlegroups.com
On 11 Feb 2013, at 7:01 PM, Jim S <j...@qlf.com> wrote:
Jonathan

I am currently using that as my base for getting this working.  Here is what I have so far:

routers = dict(
    # base router
    BASE=dict(domains = {"www.website1.com":"mustangs",
                    "www.website2.com":"icysa", }))

But, anytime I to either URL, I get the web2py welcome app.

Also, I've saved the file as routes.py.

And restarted, right?

Try turning on logging for routes and see what you get. You might also examine request.env, and make sure that the target domain is showing up properly.


-Jim

On Monday, February 11, 2013 6:32:41 PM UTC-6, Jonathan Lundell wrote:
On 11 Feb 2013, at 3:36 PM, Jim S <j...@qlf.com> wrote:
I'm trying to route traffic that comes in on a specific URL to a specifc app.

Example:

www.host1.com should route to the welcome app

www.host2.com should route to mySpecific app

I realize this is probably trivial, but I'm really struggling with it.  Hoping to do it with routes.py and not through wsgi stuff.  Please feel free to set me straight if that is not advisable.


Look at the domain-routing provision in the parametric router. Documentation in the book, and in router.example.py.

--
 



Jim Steil

unread,
Feb 11, 2013, 10:48:59 PM2/11/13
to web...@googlegroups.com
Sorry for being slow at this, route configuration is certainly not a forte of mine.  Is there something special I need to do to turn on logging?  How would I examine request.env?  I'm running all of this from pythonanywhere and don't really know where to find these things.

-Jim

--
 
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jim S

unread,
Feb 11, 2013, 11:06:20 PM2/11/13
to web...@googlegroups.com
...and yes, restarted.


On Monday, February 11, 2013 9:48:59 PM UTC-6, Jim S wrote:
Sorry for being slow at this, route configuration is certainly not a forte of mine.  Is there something special I need to do to turn on logging?  How would I examine request.env?  I'm running all of this from pythonanywhere and don't really know where to find these things.

-Jim

On Mon, Feb 11, 2013 at 9:06 
Jonathan

I am currently using that as my base for getting this working.  Here is what I have so far:

routers = dict(
    # base router
    BASE=dict(domains = {"www.website1.com":"mustangs",
                    "www.website2.com":"icysa", }))

But, anytime I to either URL, I get the web2py welcome app.

Also, I've saved the file as routes.py.
And restarted, right?

Try turning on logging for routes and see what you get. You might also examine request.env, and make sure that the target domain is showing up properly.


-Jim

On Monday, February 11, 2013 6:32:41 PM UTC-6, Jonathan Lundell wrote:
On 11 Feb 2013, at 3:36 PM, Jim S <j...@qlf.com> wrote:
I'm trying to route traffic that comes in on a specific URL to a specifc app.

Example:

www.host1.com should route to the welcome app

www.host2.com should route to mySpecific app

I realize this is probably trivial, but I'm really struggling with it.  Hoping to do it with routes.py and not through wsgi stuff.  Please feel free to set me straight if that is not advisable.


Look at the domain-routing provision in the parametric router. Documentation in the book, and in router.example.py.

--
 

--
 
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.

Jonathan Lundell

unread,
Feb 12, 2013, 12:13:23 AM2/12/13
to web...@googlegroups.com
On 11 Feb 2013, at 7:48 PM, Jim Steil <ato....@gmail.com> wrote:
Sorry for being slow at this, route configuration is certainly not a forte of mine.  Is there something special I need to do to turn on logging?  How would I examine request.env?  I'm running all of this from pythonanywhere and don't really know where to find these things.


=BEAUTIFY(request) or =BEAUTIFY(request.env) should do the trick.

Logging depends on your deployment, but it's worth figuring out. Look at logging.example.conf. You can set the loglevel of routing in routes.py.

It's really too bad that logging is such a pain to get configured, because it's really valuable.

Jim S

unread,
Feb 12, 2013, 10:25:27 AM2/12/13
to web...@googlegroups.com
So you mean to just look at it through a regular view, not in the routes.py.  Got it.  Wasn't thinking straight.

-Jim

Jim S

unread,
Feb 12, 2013, 10:48:22 AM2/12/13
to web...@googlegroups.com
Looking at request.env I'm seeing the following:


I'm routing in my routes.py based on www.myappurl.com but it never goes there.  It is always going to myaccountname.pythonanywhere.com.

-Jim

Jonathan Lundell

unread,
Feb 12, 2013, 10:58:11 AM2/12/13
to web...@googlegroups.com
On 12 Feb 2013, at 7:48 AM, Jim S <j...@qlf.com> wrote:
Looking at request.env I'm seeing the following:


I'm routing in my routes.py based on www.myappurl.com but it never goes there.  It is always going to myaccountname.pythonanywhere.com.

Interesting. That seems like a real hack on the part of Python Anywhere, and not just because of this problem, but also because you have no idea what the real referrer is. Lots of analytics tools depend on that.

--
 



Jim S

unread,
Feb 12, 2013, 11:07:19 AM2/12/13
to web...@googlegroups.com
Yes, might be a show-stopper for me and others trying to use pythonanywhere.  I was thinking there were others on the list using pythonanywhere successfully with web2py.  My problem is I know little about DNS and routing.  My DNS is hosted by mydomain.com.  There is also a good chance that I've got something screwed up there too...

-Jim

Jim Steil

unread,
Feb 12, 2013, 2:03:58 PM2/12/13
to web...@googlegroups.com
Yes, I don't believe it is a pythonanywhere problem.  I'm using mydomain for DNS hosting.  They are now telling me that I cannot setup a cname for my root domain if I'm using their mailservers and have the mx records point to them.  That sounds like a bunch of crap to me, but that is what their support is telling me.

The problem I was having earlier was when I had the DNS setup to point the url to myaccount.pythonanywhere.com.  Why were sending the traffic, but the referer was set to urlOfMyApp.com and http_host was set to myaccount.pythonanywhere.com.  So, I definitely think it is a DNS host problem but they are telling me that what I want to do is not possible, with them or any host.  I'm in no position to argue because I know little or nothing of DNS.

-Jim

On Tue, Feb 12, 2013 at 12:33 PM, Giles Thomas <giles....@gmail.com> wrote:
Hi there,

PythonAnywhere developer here.  I assume that the request environment where Jim S was seeing the incorrect http_host is the underlying WSGI environment -- is that correct?  If so, that's a weird result.  We definitely don't do anything strange and hacky with those headers; I just ran a test app to confirm and it was set to the correct domain -- that is, I saw the correct http_host, and the http_referer was unset. 

Jim, perhaps you could point me at the app that had that error?  Is there any chance that you'd set up a non-CNAME redirect at your DNS provider?  I know that Joker (our one) offers not just CNAMEs but "Web-redirects", which just does an HTTP redirect to the name you provide.  Perhaps your provider confuses the two in their interface?

Just for clarity: the link through to the username.pythonanywhere.com domain works purely at the DNS level.  We need to be able to move web apps from IP address to IP address for load balancing, so we ask our customers to set up their domain with a CNAME to username.pythonanywhere.com with their DNS provider.  But that's just a DNS thing; by the time a request from a browser gets to our servers, it's just to a specific IP address, with the appropriate Host: header in the HTTP request. 

There should definitely be no weird redirects going on; requests are routed to the appropriate WSGI app based entirely on the hostname provided in the HTTP request, and while that routing knows about which user's sandbox the request should be routed to, it knows nothing about the username.pythonanywhere.com domain.


All the best,

Giles

Massimo Di Pierro

unread,
Feb 12, 2013, 2:42:21 PM2/12/13
to web...@googlegroups.com
I am facing a similar problem. I use vps.com for DNS server and I do not see a way to redirect the top domain.

BTW. I am also using pythonanywhere (planning to move web2py there) and I noticed I had to visit:


to reload the routes. Clicking on [reload app] did not do it for me.

Massimo

Giles Thomas

unread,
Feb 12, 2013, 2:48:23 PM2/12/13
to web...@googlegroups.com
Your DNS providers aren't completely in the wrong.  The problem as I understand it is that the DNS standard itself doesn't support CNAMEs for "naked" domains, like foo.com, if you have any other data associated with them -- like mail records.  See <http://superuser.com/questions/264913/cant-set-example-com-as-a-cname-record>.  This is something that it would be great if the standards bodies fixed, but standards move slowly and there may be underlying problems that I just don't understand that would make it hard to change.

So the question is, why use CNAMEs instead of A records?  If you're using a hosting provider, it adds a level of indirection where your they can change stuff to load-balance your site without asking you to set up your own complex DNS stuff (round-robin, etc) or constantly change the IP you're pointing to.  This is useful not just for smaller sites, some of the big ones do this -- "dig www.reddit.com" for a nice example, it looks like they're using Akamai.

Anyway, I think the real underlying requirement is that lots of people want to have their site visible at both foo.com and www.foo.com.  The answer we've chosen for the PythonAnywhere website itself is to have the "official site" at www.pythonanywhere.com, and then to ask our DNS provider (Joker.com) to do an HTTP-level redirect, so that http://pythonanywhere.com/something gets redirected over to http://www.pythonanywhere.com/something.  I do that on my personal blog too -- see http://gilesthomas.com/.

This has two advantages: firstly, by making the "www" domain the official one, we can use a CNAME and load-balance ourselves like we do our customers.  Secondly, instead of having two identical sites at slightly different hostnames, we have one canonical one at one domain.  This means that all incoming links to our site go to the canonical version, which means that the Google PageRank effect of the incoming links isn't spread across two "different" domains, so we get a higher overall PageRank.  (There's also possibly an advantage in that Google apparently downrank sites that appear to just be copies of other sites, and by only having one official site then you lower the risk of that happening.)

Now, given the weirdness you saw earlier with http_referrer and http_host being wrong, it's actually quite possible that your current DNS host support an HTTP-level redirect like the one we have.  So if you set that up so that yourdomain.com does an HTTP redirect to www.yourdomain.com, and then www.yourdomain.com has a CNAME to yourusername.pythonanywhere.com, you should be set!

I hope that wasn't too rambling and incoherent...


All the best,

Giles
--
Giles Thomas
gi...@giles.net
http://www.gilesthomas.com/
http://learningwebgl.com/
http://pythonanywhere.com/

Giles Thomas

unread,
Feb 12, 2013, 2:54:24 PM2/12/13
to web...@googlegroups.com
On 12 February 2013 19:42, Massimo Di Pierro <massimo....@gmail.com> wrote:
I am facing a similar problem. I use vps.com for DNS server and I do not see a way to redirect the top domain.

BTW. I am also using pythonanywhere (planning to move web2py there) and I noticed I had to visit:


to reload the routes. Clicking on [reload app] did not do it for me.

That's odd!  Unless web2py is caching something to do with the routes in the filesystem, or somewhere else that can survive a process restart, then it definitely should force a reload -- that button basically kills and restarts all of the web app's associated processes.

One thing that might not be immediately obvious is that you have to reload each of the web apps you have set up, even if they're all pointing to the same web2py installation -- they're independent sets of server processes, and clicking the button in our web interface only reloads the one that's selected on the left-hand side of the web tab.

If that definitely wasn't the cause, I'd love to know which web app it was and roughly the timing -- then I can take a look in the logs.


All the best,

Giles

Jim Steil

unread,
Feb 12, 2013, 3:55:39 PM2/12/13
to web...@googlegroups.com
I 'think' I've just got my DNS setup they way you described.  Waiting for it to propogate....

Will check back in a few hours.

-Jim

Jim Steil

unread,
Feb 12, 2013, 6:10:08 PM2/12/13
to web...@googlegroups.com
Great news, I believe it is finally working.

For my domain, I have the root domain pointing (forwarding?) to www.domainname.com

I have a cname entry for www.domainname.com that points to username.pythonanywhere.com

for routes.py I have this setup.

 BASE=dict(
        default_application='welcome',
        domains={'www.web2pyapp1.com':'web2pyapp1',
                'www.web2pyapp2.com':'web2pyapp2'}
    ),
)

I really appreciate everyone's help with this.  Just sorry it took me so long to get my head around it.

But all is well now and I get to keep using PythonAnywhere which was a goal.

Thanks Again

-Jim
Message has been deleted

Alex Glaros

unread,
Apr 4, 2015, 7:10:10 PM4/4/15
to web...@googlegroups.com
need help with configuring pythonanywhere.com site and hostmonster.com.  


I'd like the pythonanywhere URL to instead appear to users as engagementsquared.com

Hostmonster is configured as shown in attachment. 

I created file called routes.py, put it in main engagementsquared directory in pythonanywhere.  Only content in it is:

#!/usr/bin/python
# -*- coding: utf-8 -*-
routers = dict(
    # base router
    BASE=dict(
        default_application='engagementsquared',
        domains={'www.engagementsquared.com':'engagementsquared'}
    ),
)

then restarted pythonanywhere app.

What did I do wrong?  My URL still looks like this:  https://alexglaros.pythonanywhere.com/engagementsquared/default/index

thanks,

Alex Glaros
hostmonster.png

Jim Steil

unread,
Apr 6, 2015, 11:34:53 AM4/6/15
to web...@googlegroups.com
Alex - your setup looks pretty much just like mine.  I'd contact support at pythonanywhere.  They were really helpful when I was going through this.

-Jim

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/2aAx3-6EOq8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ron Chatterjee

unread,
Apr 6, 2015, 11:41:30 AM4/6/15
to web...@googlegroups.com
When you guys resolve this, can you please share?

Alex Glaros

unread,
Apr 7, 2015, 12:45:57 PM4/7/15
to web...@googlegroups.com
it is working. Pythonanywhere.com folks were really helpful.

There was so much trial and error I don't which of the below processes was responsible for success and what might be inefficient. 

Put routes.py under web2py main directory: / > home > alexglaros > web2py > routes.py    NOT UNDER YOUR SPECIFIC APP LIKE THIS:  / > home > alexglaros > web2py > applications > engagementsquared > routes.py   
#!/usr/bin/python
# -*- codingf-8 -*-
routers = dict(
    # base router
    BASE=dict(
        default_application='engagementsquared',
        domains={'www.engagementsquared.com':'engagementsquared'}
    ),
)

create this file according to pythonanywhere.com's website directions: WSGI configuration file:
/var/www/www_engagementsquared_com_wsgi.py


import os
import sys

# add your project directory to the sys.path
project_home = u'/home/alexglaros/web2py/'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

sys.stdout = sys.stderr
os.chdir(project_home)

# serve web2py via WSGI handler
from gluon.main import wsgibase as application

At hostmonster.com, here is advice from pythonanywhere.com support:  PythonAnywhere web apps for specific domains.  If you go to www.engagementsquared.com, you'll see a PythonAnywhere "coming soon" page.  This means that you've set up the CNAME for www.engagementsquared.com correctly -- the HTTP redirect problem is only with the version without the www.  But you only have a web up set up on PythonAnywhere for alexglaros.pythonanywhere.com.  This page explains how to set things up so that you can serve an existing web app on a new domain: <https://www.pythonanywhere.com/wiki/UsingANewDomainForExistingWebApp>

Did above,then back at pythonanywhere.com site was careful to choose "manual configuration", not "create a new Web2py app". Easy to miss that.  So even though I had an app running, I still had to choose "Add a new app".

Finally, hit the "reload app" button and instant success.  Both stripped URL  "engagementsquared.com" and with a "www" www.engagmentsquared.com work.

Alex






Message has been deleted

Jim Steil

unread,
Apr 7, 2015, 3:37:09 PM4/7/15
to web...@googlegroups.com
That was it, I forgot about the wsgi file update.  That was the part I was missing as well that the pythonanywhere folks helped with.  And yes, they were pleasant to work with and had the answers.

On Tue, Apr 7, 2015 at 11:45 AM, Alex Glaros <alexg...@gmail.com> wrote:
it is working. Pythonanywhere.com folks were really helpful.

There was so much trial and error I don't which of the below processes was responsible for success and what might be inefficient. 

Put routes.py under web2py main directory: / > home > alexglaros > web2py > routes.py    NOT UNDER YOUR SPECIFIC APP LIKE THIS:  / > home > alexglaros > web2py > applications > engagementsquared > routes.py   
#!/usr/bin/python
# -*- codingf-8 -*-
routers = dict(
    # base router
    BASE=dict(
        default_application='engagementsquared',
        domains={'www.engagementsquared.com':'engagementsquared'}
    ),
)

create this file according to their website directions: WSGI configuration file:


import os
import sys

# add your project directory to the sys.path
project_home = u'/home/alexglaros/web2py/'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

sys.stdout = sys.stderr
os.chdir(project_home)

# serve web2py via WSGI handler
from gluon.main import wsgibase as application

At hostmonster.com, here is advice from pythonanywhere.com support:  PythonAnywhere web apps for specific domains.  If you go to www.engagementsquared.com, you'll see a PythonAnywhere "coming soon" page.  This means that you've set up the CNAME for www.engagementsquared.com correctly -- the HTTP redirect problem is only with the version without the www.  But you only have a web up set up on PythonAnywhere for alexglaros.pythonanywhere.com.  This page explains how to set things up so that you can serve an existing web app on a new domain: <https://www.pythonanywhere.com/wiki/UsingANewDomainForExistingWebApp>

Did above,then back at pythonanywhere.com site was careful to choose "manual configuration", not "create a new Web2py app". Easy to miss that.  So even though I had an app running, I still had to choose "Add a new app".

Finally, hit the "reload app" button and instant success.  Both stripped URL  "engagementsquared.com" and with a "www" www.engagmentsquared.com work.

Alex






Reply all
Reply to author
Forward
0 new messages