Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you disable caching for an nginx+uwsgi+python server?

I am trying to do development of a completely fresh website using exclusively nginx, uwsgi, and python. The problem is that if I change the file that uwsgi points to, and refresh the page, I get the HTML generated by the previous version of the file. It seems that nginx, uwsgi, or both are caching the code and/or response of my site. I have been reading up on uwsgi caching but have not been able to disable caching using the --cache2 flag and --cache_* flags. I found this other question and also attempted to use

proxy_no_cache "1";
proxy_cache_bypass "1";

in the server's location dictionary (located at /etc/nginx/sites-available/mysite), but alas it still caches the page. I have used web2py in the past with nginx and uwsgi and it doesn't seem to have this issue when I updated controllers or views.

This is my site file:

server { 
    listen 80 default_server; 
    listen [::]:80 default_server; 
    listen 443 ssl default_server; 
    listen [::]:443 ssl default_server; 
    include snippets/snakeoil.conf; 
    server_name _; 
    location / {
        include uwsgi_params; 
        uwsgi_pass 127.0.0.1:3031;
    }
}

this is the executed program that the init.d script uses:

uwsgi --master --processes 4 --threads 2 --die-on-term -s /tmp/mysock.sock --socket :3031 --uid www-data --gid www-data --vacuum --wsgi-file /path/to/my/python/file.py

this is the python file that is loaded:

def application(environ, start_response): 
    start_response('200 OK', [('Content-Type', 'text/html')])
    return ["<h1>Hello World</h1>"]

I understand that caching improves performance, so I intend to turn it back on for a production environment, but developing with it turned on is a pain in the butt... What am I doing wrong?

My only workaround right now is to restart the uwsgi service everytime I make a change :(

like image 908
CzechErface Avatar asked Dec 16 '25 14:12

CzechErface


2 Answers

SOLVED

Alright, I figured it out thanks to Guan Hao. His comment on web frameworks having an "auto-reload" feature set me on the path of determining how web2py works so seamlessly when modifying controllers, models, and views. As it turns out, I do have to reload uwsgi before my changes take effect and in the end I found a method (although not the best) that allows me to do development without requiring a shell to issue an /etc/init.d/mysiteapp restart everytime I make changes. According to the uWSGI documentation, there exists a --touch-reload flag that you can specify when starting uWSGI that will reload it when a certain file is modified/touched.

touch-reload
argument: required_argument
parser: uwsgi_opt_add_string_list
flags: UWSGI_OPT_MASTER
help: reload uWSGI if the specified file is modified/touched

I simply set the argument for the flag to a file called reload sitting in the root directory of the website and when I upload changes, I simply upload that file as well (at the very end). It works flawlessly! Thank you for the tip! I have two days before I can accept my own answer, so if anyone has a better method than this one, please feel free to post it.

--- UPDATE ---

It turns out there is an even better way of doing this! I stumbled across this question that has a unaccepted answer which solved the issue completely (so far). With this method I don't even have to touch the reload file. I simply added the following flag and parameter to uwsgi when it is executed in the init.d script:

--py-autoreload 1

I am not sure why I could never find the answer in Google searches and whatnot, but I could care less now that I have the perfect solution! Please beware, anyone who uses this, that you should remove the flag in production.

like image 157
CzechErface Avatar answered Dec 19 '25 03:12

CzechErface


It's not caching. You have to restart uwsgi before your changes made to python files take effect.

You may also enable the auto-reload feature of your web framework(web2py or whatever). In that case, the wsgi app will reload itself when it detects file changes, so you don't need to restart uwsgi.

like image 20
Guan Hao Avatar answered Dec 19 '25 03:12

Guan Hao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!