Flask中的SERVER_NAME主要做兩件事:
很多人誤以為它可以做這兩件事之外的其它事情。
一、第一件事:絕對URL
我們知道,url_for默認情況下是生成相對URL,它有個參數(shù)_external,如果設置為真,則會生成一個絕對URL(就是HTTP開頭帶域名等信息的)。若不指定SERVER_NAME,默認使用當前活動的請求(request)來生成URL。
下面舉個例子演示一下:
# filename myapp.py from flask import Flask, url_for app = Flask(__name__) @app.route('/') def index(): return 'hello flask' @app.route('/test') def test(): return url_for('index', _external=True) if __name__ == '__main__': app.run(debug=True)
1.【情景1】通過瀏覽器訪問
app運行之后,在本地5000端口監(jiān)聽。
(env) F:\tmp>python myapp.py * Running on http://127.0.0.1:5000/ * Restarting with reloader
若我們通過瀏覽器訪問http://127.0.0.1:5000/test,則返回的內容是:http://127.0.0.1:5000/。
若我們通過瀏覽器訪問http://localhost:5000/test,則返回的內容是:http://localhost:5000/。
可以看出,在未設置SERVER_NAME的情況下,url_for生成的絕對URL是依賴于請求的URL的。下面我們來看看不通過瀏覽器訪問的情況。
2.【情景2】非瀏覽器訪問
這個情景是指不存在request請求的情況。
我們通過Python Shell來模擬:
>>> from myapp import app >>> with app.app_context(): ... print url_for('index', _external=True) ...
Traceback (most recent call last): File "stdin>", line 2, in module> File "F:\tmp\env\lib\site-packages\flask\helpers.py", line 287, in url_for raise RuntimeError('Application was not able to create a URL ' RuntimeError: Application was not able to create a URL adapter for request indep endent URL generation. You might be able to fix this by setting the SERVER_NAME config variable.
上面的意思是說應用程序不能創(chuàng)建一個用于與request不相關的URL生成的URL適配器,可以通過設置SERVER_NAME來解決這個問題。
好,下面我們?yōu)镾ERVER_NAME設置一個值之后再試試:
>>> app.config['SERVER_NAME'] = 'example.com' >>> with app.app_context(): ... print url_for('index', _external=True) ...
http://example.com/
PS: 一般SERVER_NAME設置為網(wǎng)站的域名。
在Flask-Mail相關的文章中有這么一段話:
許多Flask的擴展都是假定自己運行在一個活動的應用和請求上下文中,F(xiàn)lask-Mail的send函數(shù)使用到current_app這個上下文了,所以當mail.send()函數(shù)在一個線程中執(zhí)行的時候需要人為的創(chuàng)建一個上下文,所有在send_async_email中使用了app.app_context()來創(chuàng)建一個上下文。
因此,若要生成不依賴于request的絕對URL(比如異步發(fā)送郵件時在郵件中生成網(wǎng)站某個頁面的URL),就必須要設置SERVER_NAME。
二、第二件事:子域名支持
SERVER_NAME鍵是用于子域名支持。因為 Flask 在得知現(xiàn)有服務器名之前不能猜測出子域名部分,所以如果你想使用子域名,這個選項必要的,并且也用于會話cookie。
請牢記不只有 Flask 存在不知道子域名的問題,你的瀏覽器同樣存在這樣的問題。 大多數(shù)現(xiàn)代 web 瀏覽器不允許服務器名不含有點的跨子域名 cookie。因此如果你的服務器的 名稱為 localhost,你將不能為 localhost 和所有它的子域名設置一個 cookie。 請選擇一個合適的服務器名,像 'myapplication.local', 并添加你想要的服務器名 + 子域名 到你的 host 配置或設置一個本地 bind。
Examples
-------->http://book.muxistudio.com || http://muxistudio.com-------->http://blog.muxistudio.com || -------->http://share.muxistudio.com
1.本地測試
修改 /etc/hosts 文件
注意:僅在本地測試中有效!
將所有需要使用的子域名添加到其中,例:
127.0.0.1 flask.dev localhost # 域名 127.0.0.1 test.flask.dev localhost # 子域名 127.0.0.1 othertest.flask.dev localhost # 子域名
在Flask應用的配置文件中添加'SERVER_NAME'
在應用配置中將'SERVER_NAME'設置為指定的域名及默認監(jiān)聽的端口,例:
#... app = Flask(__name__) app.config['SERVER_NAME'] = 'flask.dev:5000' #...
2.配置藍圖
藍圖中的subdomain為hosts文件中所添加的子域名
#... # Blueprint declaration bp = Blueprint('subdomain', __name__, subdomain="user>") #... # Register the blueprint into the application app.register_blueprint(bp) #...
3.服務器端配置
講Flask應用設置中的'SERVER_NAME'修改為生產(chǎn)環(huán)境中注冊的域名
flask.dev:5000 ----> muxistudio.com
4.Nginx配置
配置監(jiān)聽端口,下面的例子中使用正則表達式獲取用戶訪問的子域名,對于www,應該在正則表達式獲取時將其過濾,在用戶訪問時對其進行重定向至www.yourdomain.com頁面,否則www將會被視為子域名。
配置實例:
server { listen 80; listen 443 ssl; ssl_certificate /usr/local/nginx/ssl/nginx.crt; ssl_certificate_key /usr/local/nginx/ssl/nginx.key; server_name ~^www\.(?user>.+\.)?markdownblog\.com$; return 301 "$scheme://${user}markdownblog.com$request_uri"; } server { listen 80; listen 443 ssl; ssl_certificate /usr/local/nginx/ssl/nginx.crt; ssl_certificate_key /usr/local/nginx/ssl/nginx.key; server_name ~^.+\.markdownblog\.com$ markdownblog.com; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8085; } }
下一篇:Ruby的語法和語言特性總結