AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 560979
Accepted
ali haider
ali haider
Asked: 2013-12-13 09:42:25 +0800 CST2013-12-13 09:42:25 +0800 CST 2013-12-13 09:42:25 +0800 CST

使用单个 nginx 服务器来服务/代理 PHP、Python 和 NodeJS

  • 772

我试图弄清楚如何最好地使用 Nginx 作为代理来服务 PHP(通过 PHP5-FPM)、Python(通过 gunicorn)和 NodeJS。我在站点可用目录中的当前默认文件复制如下。我是否应该尝试配置多个服务器或进行其他更改以启用此功能?提前致谢。

更新: 目前,使用当前配置,Nginx 充当 NodeJS 应用程序的代理。但是,它不再提供 PHP 内容。我是否应该在默认文件中使用不同的服务器,如果是这样,我是否应该能够使用相同的侦听端口但只使用不同的 server_name 并使用位置标签来区分请求?

我正在尝试将某些 URL 请求路由到 PHP 应用程序(在 /var/www - 我从 /usr/share/nginx 切换)以及 Python 和 Nodejs 后端。

我还没有实现的一种想法是尝试多个上游并在主服务器中设置 PHP - 这是否可行,即有一个用于 NodeJS 的上游,一个用于 Python,然后是用于 PHP 的服务器。

upstream test {
        server 0.0.0.0:3002;
        keepalive 500;
}


server {
        listen 81 default_server;
        listen [::]:81 default_server; ##remove this?

        root /var/www/;  ##switched from /usr/share/nginx
        index index.php index.html index.htm;

        server_name localhost; 

        location / {
                proxy_redirect off;
                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;
                proxy_set_header Host $http_host;
                proxy_set_header X-Nginx-Proxy true;
                proxy_set_header Connection "";
                proxy_http_version 1.1;
                proxy_pass http://0.0.0.0:3002;
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                allow ::1;
                deny all;
        }

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        location /RequestDenied {
                proxy_pass http://127.0.0.1:4242;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
server {
        listen 82;
        root /var/www/;
        index index.php index.html index.htm;
        server_name php;
        location ~ /testPHP {    //testPHP is part of URL/directory name in /var/www/
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

}
nginx
  • 2 2 个回答
  • 4506 Views

2 个回答

  • Voted
  1. Best Answer
    ali haider
    2013-12-27T12:43:39+08:002013-12-27T12:43:39+08:00

    不确定这是否是最好的接近方式,但它帮助我实现了我想要的。我只是为代理创建了一个新的服务器设置,并使用一个服务器来提供 php 内容。

    upstream test {
            server 0.0.0.0:3002;
            keepalive 500;
    }
    
    
    server {
            listen 81 default_server;
            listen [::]:81 default_server; ##remove this?
    
            root /var/www/;  ##switched from /usr/share/nginx
            index index.php index.html index.htm;
    
            server_name localhost; 
    
            location / {
                    proxy_redirect off;
                    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;
                    proxy_set_header Host $http_host;
                    proxy_set_header X-Nginx-Proxy true;
                    proxy_set_header Connection "";
                    proxy_http_version 1.1;
                    proxy_pass http://0.0.0.0:3002;
            }
    
            location /doc/ {
                    alias /usr/share/doc/;
                    autoindex on;
                    allow 127.0.0.1;
                    allow ::1;
                    deny all;
            }
    
            # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
            location /RequestDenied {
                    proxy_pass http://127.0.0.1:4242;
            }
    }
    
    
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    server {
            listen 82;
            root /var/www/;
            index index.php index.html index.htm;
            server_name php;
    
            location / {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
    
    } 
    
    • 2
  2. John P
    2014-06-16T00:26:27+08:002014-06-16T00:26:27+08:00

    我一直在寻找类似的解决方案,在 Nginx 中使用 FastCGI 和 NodeJS 服务器提供 PHP。但是我想为来自同一个域和端口的所有请求提供服务。我解决了基于位置的代理请求,而不是拥有单独的服务器。

    此示例使用上游:

    upstream nodejs {
        server 127.0.0.1:8080 max_fails=0;
    }
    
    server {     
        listen 80;
        server_name _;
        set $root /var/www/sitename;
        root $root;
        index.php index index.html;
    
        access_log /var/www/sitename/logs/access.log;
        error_log /var/www/sitename/logs/error.log;
    
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:8888;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $root/$fastcgi_script_name;
            fastcgi_param  PATH_INFO $fastcgi_script_name;
    
            include /etc/nginx/fastcgi_params;
    
            if (!-e $request_filename) {
                rewrite ^(.+)$ /index.php?q=$1 last;
                break;
            }
        }
    
        location /nodejs/ {
            proxy_pass  http://nodejs;
            proxy_redirect off;
    
            proxy_set_header Host $host ;
            proxy_set_header X-Real-IP $remote_addr ;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
        }
    }
    

    我发现这篇文章很有用,而且这段代码更像是一个克隆:

    http://blog.i-evaluation.com/2013/04/05/lannn-setting-up-nginx-with-php-and-node-js-on-aws/

    • 2

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve