Nginx:
假设已经有一个运行着python程序的Flask程序在运行了,试通过nginx转发,去实现网页的访问
1、安装windows版本的nginx
2、配置nginx:
在server节点中新增一个location】一个upstream节点(注意层级):
http{
......
server {
......
location /Test{
proxy_pass http://upstreamname/; #这里面的最后的斜杠一定不能少
}
}
upstream upstreamname{
server http://120.0.0.1:5000;
}
}
upstream 和server同级!!!
3、启动flask的python程序。
4、启动nginx(双击exe文件即可)
5、在浏览器输入nginx服务器地址,加上/Test,即可被nginx转发到对应location所对应的upstream中对应的地址中的资源
6、网页运行结果截图:

附件:程序源文件
nginx文件:
#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,#html;
}
location /Test {
proxy_pass http://demo/;
}
#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;
#}
}
# 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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
upstream demo{
server 127.0.0.1:5000;
}
}
python程序文件:
# 导入Flask 包
from flask import Flask, redirect, url_for
# 声明Flask的一个对象
app = Flask(__name__)
# 创建路由1
@app.route('/')
def hello_world():
return "Hello World created by pychrom!"
# 创建路由2
@app.route('/guoge/')
def custom():
return str(3 + 2)
# 创建带有字符串变量的路由
@app.route('/hello/<name>/')
def sayHello(name):
return "say hello to %s" % name
# 创建带有int类型的变量flaot、path的同样也可以
@app.route('/number/<int:number>/')
def number(number):
return "number is %d" % number
# URL构建
@app.route('/admin/<admin>')
def admin_type(admin):
return "current admin user is %s" % admin
@app.route('/guest/<guest>')
def guest_type(guest):
return "current guest user is %s " % guest
# url构建所要访问的主函数
@app.route('/user/<type>/<name>')
def which_user(type, name):
if type == 'admin':
return redirect(url_for('admin_type', admin=name))
else:
return redirect(url_for('guest_type', guest=name))
# run
if (__name__ == '__main__'):
app.run();
本文介绍了如何在Windows上安装Nginx并配置反向代理,以转发请求到运行中的Flask应用程序。通过设置nginx的upstream和location节点,成功启动Nginx和Flask后,可以通过在浏览器输入特定URL来访问Flask服务。
1440

被折叠的 条评论
为什么被折叠?



