简体   繁体   English

如何禁用Nginx + uwsgi + python服务器的缓存?

[英]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. 我正在尝试仅使用nginx,uwsgi和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. 问题是,如果我更改uwsgi指向的文件并刷新页面,则会得到文件先前版本生成的HTML。 It seems that nginx, uwsgi, or both are caching the code and/or response of my site. 看来nginx,uwsgi或两者都在缓存我的网站的代码和/或响应。 I have been reading up on uwsgi caching but have not been able to disable caching using the --cache2 flag and --cache_* flags. 我一直在阅读uwsgi缓存,但无法使用--cache2标志和--cache_*标志禁用缓存。 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. 在服务器的位置字典中(位于/ etc / nginx / sites-available / mysite),但是可惜它仍然可以缓存页面。 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. 过去,我曾将Web2py与nginx和uwsgi结合使用,并且在更新控制器或视图时似乎没有此问题。

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: 这是init.d脚本使用的已执行程序:

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: 这是加载的python文件:

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 :( 我目前唯一的解决方法是每次更改时都重新启动uwsgi服务:(

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. 他对具有“自动重载”功能的Web框架的评论为我确定了在修改控制器,模型和视图时web2py如何如此无缝地工作的道路。 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. 事实证明,在更改生效之前,我必须重新加载uwsgi,最后我找到了一种方法(尽管不是最好的),该方法可以使我进行开发而无需shell发出/etc/init.d/mysiteapp restart每当我进行更改时, /etc/init.d/mysiteapp restart 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. 根据uWSGI文档,存在一个--touch-reload标志,您可以在启动uWSGI时指定它,当修改/触摸某个文件时,它将重新加载它。

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). 我只是将标志的参数设置为一个名为reload的文件,该文件位于网站的根目录中,当我上传更改时,我也只需上传该文件(最后)。 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. 使用这种方法,我什至不必触摸reload文件。 I simply added the following flag and parameter to uwsgi when it is executed in the init.d script: 在init.d脚本中执行时,我只是向uwsgi添加了以下标志和参数:

--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! 我不确定为什么我永远无法在Google搜索中找到答案,但现在有了完善的解决方案,我就不必担心了! Please beware, anyone who uses this, that you should remove the flag in production. 请注意,任何使用此标志的人都应删除生产中的标志。

It's not caching. 它不是缓存。 You have to restart uwsgi before your changes made to python files take effect. 您必须重新启动uwsgi才能对python文件所做的更改生效。

You may also enable the auto-reload feature of your web framework(web2py or whatever). 您还可以启用Web框架的自动重新加载功能(web2py或其他功能)。 In that case, the wsgi app will reload itself when it detects file changes, so you don't need to restart uwsgi. 在这种情况下,wsgi应用程序在检测到文件更改时将重新加载自身,因此您无需重新启动uwsgi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM