云服务器部署基础(2)

二、实现网页访问

找到路径/etc/nginx/sites-available的配置文件,以下添加服务,将请求反向代理到本机的5000端口,也就是flask运行的端口:(其中的ip地址改为实际公网ip)

server {
    listen 80;
    server_name 47.98.180.149;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

以下为一个简单的python示例,保存到 ~/app.py

from flask import Flask, request, render_template, redirect, url_for

app = Flask(__name__)

# 首页
@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

保存在与 app.py 同级的 templates 文件夹中,路径为 ~/templates/index.html 。下面是一个简单示例:

<html>
    <head>
        <title>第一个页面</title>
    </head>
    <body>
       hello world
    </body>
</html>
← 返回首页