Nginx增添api接口记录

问题

在之前的文章中《Flask 部署项目 Nginx + Gunicorn + Flask》有讲解如何配置Nginx+Gunicorn+Flask服务。现需求是需要增加一个接口。

方法

需要重新修改 Nginx 的配置文件(/etc/nginx/nginx.conf 或其他自定义的配置文件), 添加一个新的 server 块或者修改现有的 server 块。下面是一个简单的例子,展示了如何在配置文件中添加一个新的接口:

  1. 打开 Nginx 配置文件:
sudo nano /etc/nginx/nginx.conf
  1. 在文件中找到 http 块。通常,它位于文件的末尾。在 http 块中,添加一个新的 server 块,如下所示:
http {
    ...

    server {
        listen 80; # 确保选择一个未被占用的端口
        server_name example.com; # 使用你的域名或 IP 地址替换

        location / {
            proxy_pass http://localhost:8080; # 将请求转发到后端应用程序,如 Node.js、Python 等
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}
  1. 保存文件并退出编辑器。

  2. 检查配置文件的语法是否正确:

sudo nginx -t

如果一切正常,看到以下输出:

nginx: configuration file /etc/nginx/nginx.conf test is successful
  1. 重新加载 Nginx 以应用更改:
sudo systemctl reload nginx

现在,新接口应该已经生效。访问 http://example.com(将其替换为实际域名或 IP 地址)以测试新接口。

注意,根据实际需求和应用程序,可能需要根据实际情况调整 serverlocation 块中的配置。以上示例仅供参考。

猜你喜欢

转载自blog.csdn.net/uncle_ll/article/details/131093674