手机版 收藏 导航

Nginx如何配置Web应用程序,如PHP、Python、Node.js等

原创   www.link114.cn   2025-04-26 20:33:19

Nginx如何配置Web应用程序,如PHP、Python、Node.js等

对于PHP应用程序,我们需要在Nginx配置文件中添加以下内容:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
这样可以让Nginx将PHP请求转发给PHP-FPM处理。还需要配置FastCGI参数并启用PHP解析。

对于Python应用程序,我们可以使用Gunicorn作为WSGI服务器,并在Nginx中添加如下配置:

location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
这样可以让Nginx将请求转发给运行在8000端口的Gunicorn进程。

对于Node.js应用程序,我们可以使用PM2作为进程管理器,并在Nginx中添加如下配置:

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
这样可以让Nginx将请求转发给运行在3000端口的Node.js应用程序。