㈠ flask 藍圖nginx部署後怎麼訪問靜態資源
能共復存,不需要聯系的話制兩個用不一樣的埠就行 比如nginx用80,apache用8080 如果都想用8080訪問,那麼可以把nginx作為apache的前端 兩種方法 1、直接用nginx反代的方式 2、靜態交給nginx處理,PHP交給apache處理
㈡ Flask 框架怎麼處理多級靜態目錄
可以自己寫filter解決,靜態的路徑可以在req之前在context添加,例如:
def req_ctx_config(app, static_path):
@app.before_request
def create_app_config():
if not hasattr(g, 'static_path'):
g.static_path= static_path
然後你就可以在渲版染裡面使用g.static_path獲得靜態根目權錄了,如果配合filter,可以實現類似
{{g.static_path|to_static_url('images', '1.jpg')}}
這種模式
㈢ 怎麼使用python flask搭建靜態伺服器
Frozen-Flask freezes aFlaskapplication into a set of static files. The result can be hosted without any server-side software other than a traditional web server.
Note:This project used to be called Flask-Static.
Installation
Install the extension with one of the following commands:
$ easy_install Frozen-Flask
or alternatively if you have pip installed:
$ pip install Frozen-Flask
or you can get thesource code from github.
Context
This documentation assumes that you already have a workingFlaskapplication. You can run it and test it with the development server:
from myapplication import appapp.run(debug=True)
Frozen-Flask is only about deployment: instead of installing Python, a WGSI server and Flask on your server, you can use Frozen-Flask tofreezeyour application and only have static HTML files on your server.
Getting started
Create aFreezerinstance with yourappobject and call itsfreeze()method. Put that in afreeze.pyscript (or call it whatever you like):
from flask_frozen import Freezerfrom myapplication import appfreezer = Freezer(app)if __name__ == '__main__':
freezer.freeze()
This will create abuilddirectory next to your application』, with your application』s content frozen into static files.
Note
Frozen-Flask considers it 「owns」 its build directory. By default, it willsilently overwritefiles in that directory, andremovethose it did not create.
Theconfigurationallows you to change the destination directory, or control what files are removed if at all.
This build will most likely be partial since Frozen-Flask can only guess so much about your application.
Finding URLs
Frozen-Flask works by simulating requests at the WSGI level and writing the responses to aptly named files. So it needs to find out which URLs exist in your application.
The following URLs can be found automatically:
Static files handled by Flask for your application or any of itsblueprints.
Views with no variable parts in the URL, if they accept theGETmethod.
New in version 0.6:Results of calls toflask.url_for()made by your application in the request for another URL. In other words, if you useurl_for()to create links in your application, these links will be 「followed」.
This means that if your application has an index page at the URL/(without parameters) and every other page can be found from there by recursively following links built withurl_for(), then Frozen-Flask can discover all URLs automatically and you』re done.
Otherwise, you may need to write URL generators.
URL generators
Let』s say that your application looks like this:
If, for some reason, some procts pages are not linked from another page (or these links are not built byurl_for()), Frozen-Flask will not find them.
To tell Frozen-Flask about them, write an URL generator and put it after creating yourFreezerinstance and before callingfreeze():
Frozen-Flask will find the URL by callingurl_for(endpoint,**values)whereendpointis the name of the generator function andvaluesis each dict yielded by the function.
You can specify a different endpoint by yielding a(endpoint,values)tuple instead of justvalues, or you can by-passurl_forand simply yield URLs as strings.
Also, generator functions do not have to bePython generatorsusingyield, they can be any callable and return any iterable object.
All of these are thus equivalent:
Generating the same URL more than once is okay, Frozen-Flask will build it only once. Having different functions with the same name is generally a bad practice, but still work here as they are only used by their decorators. In practice you will probably have a mole for your views and another one for the freezer and URL generators, so having the same name is not a problem.
Testing URL generators
The idea behind Frozen-Flask is that you canuse Flask directlyto develop and test your application. However, it is also useful to test yourURL generatorsand see that nothing is missing, before deploying to a proction server.
You can open the newly generated static HTML files in a web browser, but links probably won』t work. TheFREEZER_RELATIVE_URLSconfigurationcan fix this, but adds a visibleindex.htmlto the links. Alternatively, use therun()method to start an HTTP server on the build result, so you can check that everything is fine before uploading:
Freezer.run()will freeze your application before serving and when the reloader kicks in. But the reloader only watches Python files, not templates or static files. Because of that, you probably want to useFreezer.run()only for testing the URL generators. For everything else use the usualapp.run().
Flask-Scriptmay come in handy here.
Controlling What Is Followed
Frozen-Flask follows links automatically or with some help from URL generators. If you want to control what gets followed, then URL generators should be used with the Freezer』swith_no_argument_rulesandlog_url_forflags. Disabling these flags will force Frozen-Flask to use URL generators only. The combination of these three elements determines how much Frozen-Flask will follow.
Configuration
Frozen-Flask can be configured using Flask』sconfiguration system. The following configuration values are accepted:
FREEZER_BASE_URL
Full URL your application is supposed to be installed at. This affects the output offlask.url_for()for absolute URLs (with_external=True) or if your application is not at the root of its domain name. Defaults to'http://localhost/'.
FREEZER_RELATIVE_URLS
If set toTrue, Frozen-Flask will patch the Jinja environment so thaturl_for()returns relative URLs. Defaults toFalse. Python code is not affected unless you userelative_url_for()explicitly. This enables the frozen site to be browsed without a web server (opening the files directly in a browser) but appends a visibleindex.htmlto URLs that would otherwise end with/.
New in version 0.10.
FREEZER_DEFAULT_MIMETYPE
The MIME type that is assumed when it can not be determined from the filename extension. If you』re using the Apache web server, this should match theDefaultTypevalue of Apache』s configuration. Defaults toapplication/octet-stream.
New in version 0.7.
FREEZER_IGNORE_MIMETYPE_WARNINGS
If set toTrue, Frozen-Flask won』t show warnings if the MIME type returned from the server doesn』t match the MIME type derived from the filename extension. Defaults toFalse.
New in version 0.8.
FREEZER_DESTINATION
Path to the directory where to put the generated static site. If relative, interpreted as relative to the application root, next to . Defaults tobuild.
FREEZER_REMOVE_EXTRA_FILES
If set toTrue(the default), Frozen-Flask will remove files in the destination directory that were not built ring the current freeze. This is intended to clean up files generated by a previous call toFreezer.freeze()that are no longer needed. Setting this toFalseis equivalent to settingFREEZER_DESTINATION_IGNOREto['*'].
New in version 0.5.
FREEZER_DESTINATION_IGNORE
A list (defaults empty) offnmatchpatterns. Files or directories in the destination that match any of the patterns are not removed, even ifFREEZER_REMOVE_EXTRA_FILESis true. As in.gitignorefiles, patterns apply to the whole path if they contain a slash/, to each slash-separated part otherwise. For example, this could be set to['.git
㈣ nginx+gunicorn+flask部屬web時,使用nginx如何指定多個靜態文件路徑
只能使用不同的名字,比如用location /static2{.....