window下flask+uwsgi

1.写在前面

    一般情况下,flask回部署在linux上,且以flask+uWSGI+Nginx的形式,由于公司项目环境限制,所以flask写的web需要部署在windows上,且要用nginx。无感! 查资料有的说,windows下没有uWSGI,还有的是其他方式部署,总之,很头疼。于是乎,省略掉了uwsgi,直接用了flask自带的服务,采用flask+Nginx的方式部署,不搭建uWSGI了

2.下载Nginx

   官网下载地址:http://nginx.org/en/download.html

选择最新的下载即可,如下图

141914_eVJV_1455020.png

 

下载完成后,解压到自定义目录,文件目录如下:

142200_13Wl_1455020.png

 

3.配置nginx,使其有效的代理flask服务。

进入conf文件夹,打开nginx.conf配置文件。我们接下来需要编辑该文件,建立flask和nginx的联系。

配置文件的初始内容如下:

 
  1. #user nobody;
  2. worker_processes 1;

  3.  
  4. #error_log logs/error.log;

  5. #error_log logs/error.log notice;

  6. #error_log logs/error.log info;

  7.  
  8. #pid logs/nginx.pid;

  9.  
  10.  
  11. events {

  12. worker_connections 1024;

  13. }

  14.  
  15.  
  16. http {

  17. include mime.types;

  18. default_type application/octet-stream;

  19.  
  20. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '

  21. # '$status $body_bytes_sent "$http_referer" '

  22. # '"$http_user_agent" "$http_x_forwarded_for"';

  23.  
  24. #access_log logs/access.log main;

  25.  
  26. sendfile on;

  27. #tcp_nopush on;

  28.  
  29. #keepalive_timeout 0;

  30. keepalive_timeout 65;

  31.  
  32. #gzip on;

  33.  
  34. server {

  35. listen 80;

  36. server_name localhost;

  37.  
  38. #charset koi8-r;

  39.  
  40. #access_log logs/host.access.log main;

  41.  
  42. location / {

  43. root html;

  44. index index.html index.htm;

  45. }

  46.  
  47. #error_page 404 /404.html;

  48.  
  49. # redirect server error pages to the static page /50x.html

  50. #

  51. error_page 500 502 503 504 /50x.html;

  52. location = /50x.html {

  53. root html;

  54. }

  55.  
  56. # proxy the PHP scripts to Apache listening on 127.0.0.1:80

  57. #

  58. #location ~ \.php$ {

  59. # proxy_pass http://127.0.0.1;

  60. #}

  61.  
  62. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

  63. #

  64. #location ~ \.php$ {

  65. # root html;

  66. # fastcgi_pass 127.0.0.1:9000;

  67. # fastcgi_index index.php;

  68. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

  69. # include fastcgi_params;

  70. #}

  71.  
  72. # deny access to .htaccess files, if Apache's document root

  73. # concurs with nginx's one

  74. #

  75. #location ~ /\.ht {

  76. # deny all;

  77. #}

  78. }

  79.  
  80.  
  81. # another virtual host using mix of IP-, name-, and port-based configuration

  82. #

  83. #server {

  84. # listen 8000;

  85. # listen somename:8080;

  86. # server_name somename alias another.alias;

  87.  
  88. # location / {

  89. # root html;

  90. # index index.html index.htm;

  91. # }

  92. #}

  93.  
  94.  
  95. # HTTPS server

  96. #

  97. #server {

  98. # listen 443 ssl;

  99. # server_name localhost;

  100.  
  101. # ssl_certificate cert.pem;

  102. # ssl_certificate_key cert.key;

  103.  
  104. # ssl_session_cache shared:SSL:1m;

  105. # ssl_session_timeout 5m;

  106.  
  107. # ssl_ciphers HIGH:!aNULL:!MD5;

  108. # ssl_prefer_server_ciphers on;

  109.  
  110. # location / {

  111. # root html;

  112. # index index.html index.htm;

  113. # }

  114. #}

  115.  
  116. }

如果只是简单的配置,运行。我们需要改动的地方很少。其他基本默认即可。 需要改动的地方是http节点下面的server节点内容。改动截图如下:

142801_IEJq_1455020.png

servername 如果填写ip,或者域名都可以。  location节点填写的内容,proxy_pass http://127.0.0.1:5000 实际上对应的是flask程序启动后的web地址。  这样对应起来后,我们启动flask后,再启动nginx服务,就可以直接在本机输入服务ip地址(不用加端口)来访问我们的网站了。事实上,nginx利用默认的80端口,代理了5000端口的web服务。做到了反向代理转发的作用。

5.nginx服务的常用指令

注意不要直接双击nginx.exe,这样会导致修改配置后重启、停止nginx无效,需要手动关闭任务管理器内的所有nginx进程

 

在nginx.exe目录,打开命令行工具,用命令 启动/关闭/重启nginx 

 

start nginx : 启动nginx

nginx -s reload  :修改配置后重新加载生效

nginx -s reopen  :重新打开日志文件
nginx -t -c   /path/to/nginx.conf 测试nginx配置文件是否正确

关闭nginx:
nginx -s stop  :快速停止nginx
nginx -s quit  :完整有序的停止nginx

 

 

转载于:https://my.oschina.net/RabbitXiao/blog/1788605

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值