Windows系統(tǒng)下使用flup搭建Nginx和Python環(huán)境的方法
首先確保你的電腦里已經(jīng)安裝了Python和Django,接下來我們還需要兩個(gè)組件,nginx服務(wù)器和flup(Python的FastCGI組件)
nginx下載地址:http://nginx.org/en/download.html
flup下載地址:http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz
與Linux下不同的是,nginx在windows下是以一個(gè)應(yīng)用程序的方式運(yùn)行,而不是以一個(gè)服務(wù)運(yùn)行(難怪沒人在windows服務(wù)器上用nginx)
把剛剛下載好的兩個(gè)壓縮包都解壓到C:\nginx\, C:\flup\(目錄可自己選擇,這里只做個(gè)演示)然后用python setup.py install 命令
安裝flup,接著就要配置nginx了,打開C:\nginx\conf\nginx.conf,我的配置文件如下,大家可根據(jù)需要自行修改:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
# 靜態(tài)資源
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|js)$
{
root e:/gin/gin/;
expires 30d;
break;
}
location ~ ^/static/ {
root e:/gin/gin/;
expires 30d;
break;
}
location ~ ^/ {
# 指定 fastcgi 的主機(jī)和端口
fastcgi_pass 127.0.0.1:8051;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
需要注意的是,對于不需要url rewrite的目錄,比如存放css和圖片的目錄,需要在配置文件里指明,否則將無法訪問這些文件
location ~ ^/static/ {
root e:/gin/gin/;
expires 30d;
break;
}
最后一步就是運(yùn)行nginx服務(wù)器,并且用FastCGI運(yùn)行你的Django項(xiàng)目了
進(jìn)入nginx的目錄:
cd c:\nginx\ start nginx
然后在瀏覽器里訪問http://loaclhost/ 就應(yīng)該可以看到nginx的歡迎界面了。最后進(jìn)入你的Django項(xiàng)目的根目錄,然后用一下命令來運(yùn)行服務(wù)器:
python manage.py runfcgi method=threaded host=127.0.0.1 port=8051
刷新localhost頁面,你就能看到你的項(xiàng)目主頁啦~~
補(bǔ)充一點(diǎn)windwos下nginx操作的命令(來自官方文檔)
nginx -s stop quick exit nginx -s quit graceful quit nginx -s reload changing configuration, starting a new worker, quitting an old worker gracefully nginx -s reopen reopening log files
大功告成,開始django之旅,ohye?。?!
相關(guān)文章
超詳細(xì)注釋之OpenCV旋轉(zhuǎn)圖像任意角度
這篇文章主要介紹了OpenCV旋轉(zhuǎn)圖像任意角度,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Python學(xué)習(xí)筆記之lambda表達(dá)式用法詳解
這篇文章主要介紹了Python學(xué)習(xí)筆記之lambda表達(dá)式用法,結(jié)合實(shí)例形式詳細(xì)分析了lambda表達(dá)式的概念、功能、原理、組成及相關(guān)使用技巧,需要的朋友可以參考下2019-08-08
跟老齊學(xué)Python之一個(gè)免費(fèi)的實(shí)驗(yàn)室
學(xué)習(xí)Python也要做實(shí)驗(yàn),也就是嘗試性地看看某個(gè)命令到底什么含義。在《集成開發(fā)環(huán)境(IDE)》一章中,我們介紹了Python的IDE時(shí),給大家推薦了IDLE,進(jìn)入到IDLE中,看到>>>符號(hào),可以在后面輸入一行指令。其實(shí),這就是一個(gè)非常好的實(shí)驗(yàn)室。2014-09-09
Python 使用元類type創(chuàng)建類對象常見應(yīng)用詳解
這篇文章主要介紹了Python 使用元類type創(chuàng)建類對象,結(jié)合實(shí)例形式詳細(xì)分析了Python元類的概念、功能及元類type創(chuàng)建類對象的常見應(yīng)用技巧,需要的朋友可以參考下2019-10-10

