Welcome! Log In Create A New Profile

Advanced

nginx in front of CherryPy - works partly but can't login, some images don't show initially

Posted by StefanScott 
Hi -

I am trying to use a webserver called TiddlySpace
which uses CherryPy (Python web server).

I am able to run this fine using just CherryPy.

When I use nginx as a front-end proxy, it works "partially".

I am posting these questions here (with my nginx.conf file below)
in case someone may be able to help. Thanks!

===

I have installed TiddlySpace and everything runs fine
when serving via CherryPy alone,
at http://xyz.vpsprovider.com:8080
(where xyz.vpsprovider.com represents the hostname assigned by my VPS hosting provider).

Now I want to:
(a) use a different (sub)domain which I own (eg, test001.mydomain.com) and
(b) use port 80,
so I am testing using CherryPy with nginx as a front-end proxy -
and it works, but only partly:

(1) some images (which are embedded using base64 encoding)
don't display (until the page is refreshed), and

(2) I can't log in anymore.

I realize that this forum is not for CherryPy or Python,
but maybe someone has some experience using nginx and CherryPy
and could provide some ideas.

+++

(1) Serving with CherryPy (arbitrarily choosing host &
port http://0.0.0.0:7000, on Linux) behind nginx front-end proxy,
everything on the page displays correctly except an icon
whose location (in the page source) is:

/bags/tiddlyspace/tiddlers/publicIcon

I understand this icon may be a bit unusual, because looking at the page source,
it is embedded directly in the page using <pre>...</pre> tags
containing about 3000 base64-encoded characters. (!)

Strangely enough, if I REFRESH the page,
the icon always appears after the refresh.

It's only missing the first time I display the page -
even if I display the page in a new window / tab
of the same browser during the same browser session.

So the first displaying of the page in a new tab or window
doesn't show the icon, and when I hit REFRESH it does show the icon.

===

More details: on the FIRST displaying of the page:
if I right-click the broken icon and say 'Open image in new tab',
it tries to go to:

http://0.0.0.0:7000/bags/tiddlyspace/tiddlers/publicIcon

and my Chrome browser says "This webpage is not available".

On the SECOND displaying of the page,
if I right-click the now-good icon and say
"Open image in new tab", it goes to:

data:image/png;base64,iVBORw0KGgo...K5CYII=

and shows the correct icon (the circle).

[I have elided 2,794 characters inside that data: URL!]

(TiddlySpace is unusual - it is a single-page app which does almost nothing
on the server - almost all the code is JavaScript in a single page of HTML,
which also contains some images encoded using base64.)

+++

(2) Trying to with CherryPy behind nginx proxy,
when I attempt to login to TiddlySpace with my username and password, it says:

an error occurred
try again

+++

I've used nginx as a proxy in front of several other servers
(node.js, golang, php-fpm) but never in front of CherryPy -
so I probably haven't configured my nginx.conf file properly for CherryPy
(see below for a listing).

I like Python and I've read a few manuals about it by Guido Rossum a few years ago,
but I have no hands-on Python experience
and I don't know much yet about the web frameworks used in the Python world.

I understand that the TiddlySpace server uses CherryPy with WSGI,
but this is the first I've ever heard of WSGI.

Earlier, someone sent me a link about uWSGI and nginx:

http://projects.unbit.it/uwsgi/wiki/Quickstart

I have looked at that and tried to do what it says.
It mentions something called 'Flask' also -
I'm not sure if the TiddlySpace server is using Flask.

In the section about nginx, that link says:

QUOTE: "When you want to put uWSGI behind a full webserver
(read: proxies configuration), the best solution is using the uwsgi protocol
instead of the http one. You will remove the impact of
the (relatively-slow) http parsing, and you will get
a set of features derivated by the use of a specialized protocol."

Question re uWSGI: If I want to switch from http protocol to uwsgi protocol,
how would I do that? Would it allow me to log in?

+++

Here is my config file:

/home/tiddlyspace/tiddlyspace_instance/tiddlywebconfig.py

when using CherryPy and nginx front-end proxy
(which works but doesn't display some icons initially, and doesn't let me log in):

config = {
'system_plugins': ['tiddlywebplugins.tiddlyspace'],
'secret': '123very456long123string789and456numbers',
'twanager_plugins': ['tiddlywebplugins.tiddlyspace'],
'server_host': {
'scheme': 'http', # or https
'host': '0.0.0.0',
'port': '7000'
}
}

Hmm... seeing 'scheme': 'http' seems to mean that
I am using http protocol. According to the QUOTE above,
it might be better to use uWSGI.

However, I have no idea if I can simply replace http with uwsgi in the above config file!

===

And here is (the relevant parts of) my file /opt/nginx/conf/nginx.conf -
which works partially with CherryPy
(except for some icons not appearing on first displaying of page, and also I cannot login):

#user www www;
worker_processes 5;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
worker_connections 4096;
}

http {
include mime.types;
index index.html;
default_type application/octet-stream;
access_log logs/access.log main;
tcp_nopush on;

sendfile on;

# http://stackoverflow.com/questions/2272931/cherrypy-behind-nginx-reverse-proxy-post-requests-corrupted-truncated
# tested 2 ways: with and without the following line:
ignore_invalid_headers on;

# ...

server {
listen 80;
server_name .test001.mydomain.com;
access_log /var/log/www/test001.mydomain.com.access.log main;
error_log /var/log/www/test001.mydomain.com.error.log;

# root /srv/www/test001.mydomain.com;

location / {
# tested 2 ways: with and without the following line:
# include uwsgi_params;
proxy_pass http://0.0.0.0:7000;

# http://xhtml.net/scripts/Django-CherryPy-server-DjangoCerise
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}

}

# ...
}

===

And here is /opt/nginx/conf/uwsgi_params, as provided with nginx (version 0.8.53) :

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;

uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;

uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

+++

I see a question on StackOverflow
where they say there were some problems doing POST with nginx and CherryPy:

http://stackoverflow.com/questions/2272931/cherrypy-behind-nginx-reverse-proxy-post-requests-corrupted-truncated

This might be relevant to what I'm doing here,
since when you login you are doing a POST.

+++

The example domain 'mydomain.com' is just one of several domains I own.

My server is a VPS which has a hostname of its own as well,
eg: xyz.vpsprovider.com - assigned by the VPS hosting provider.

Yesterday I was able to use CherryPy alone for TiddlySpace
with the following config which uses my server's "main" xyz.vpsprovider.com
and port 8080:

config = {
'system_plugins': ['tiddlywebplugins.tiddlyspace'],
'secret': '123very456long123string789and456numbers',
'twanager_plugins': ['tiddlywebplugins.tiddlyspace'],
'server_host': {
'scheme': 'http', # or https
'host': 'xyz.vpsprovider.com',
'port': '8080'
}
}

This worked fine - all images displayed on the first displaying of the page,
and I was able to log in and make changes to the page.

+++

But I would like to:

(a) be able to use various (sub)domains which I own (eg,test001.mydomain.com)
- instead of the "main" hostname (xyz.vpsprovider.com)
which was assigned by the VPS hosting provider, and

(b) use port 80 instead of 8080.

So right now I can only use CherryPy with my VPS's "main" hostname
(xyz.vpsprovider.com) and port 8080, but so far I can't:

- use nginx as a front-end proxy to CherryPy (it partly works,
but I can't login, and some images don't show), or

- use a (sub)domain which I own (eg,test001.mydomain.com) and port 80
via CherryPy alone (CherryPy returns an error)

+++

Sorry for this long-winded question -
I just never know what part of my configuration is wrong,
so I wanted to provide the details,
in case anyone can diagnose what I'm doing wrong here.

Anyways, thanks for any help!

- Scott
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 142
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready