我的所有代码都在 django 中,因此使用 gunicorn 来托管 django,并使用 nginx 作为反向代理。 我现在想使用 nginx 服务器作为反向代理和 apache 服务器在同一域(或子域)上托管一些 PHP 文件。
是否可以为 2 个不同的服务器反向代理 nginx? (如果 gunicorn 失败,试试 apache)
我可以直接在apache上托管php文件,但是80端口被nginx占用了。假设我想使用 apache 在 blog.XXXX.com 上托管一个基于 php 的博客应用程序-wordpress?以及我在 XXXX.com 上使用 nginx 和 gunicorn 的常规网站
这里是我当前nginx.conf的相关部分
server {
listen 80;
server_name XXXXX.com;
root /home/ubuntu/code/;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 120;
proxy_read_timeout 120;
proxy_pass http://localhost:8000/;
}
}
最佳答案
是的,这是可能的。不过,您需要将 Apache 配置为监听不同的端口。
不要选择保留端口并确保它是开放的(并且你可以访问它。Unix 有一个端口范围,如果你选择 ipv4,通常在 /proc/sys/net/ipv4/ip_local_port_range
中定义,你可能已经通过你的 Nginx 配置判断了)
对于 Apache 配置:
编辑你的 apache 主配置文件并设置 Listen
指令如下:
Listen 127.0.0.1:<port number that's open>
//开始使用8080作为端口
然后创建一个VirtualHost如下:
<VirtualHost 127.0.0.1:<port number defined above>>
config goes here
</VirtualHost>
您可以通过两种方式执行此操作,一种来自同一域,一种来自子域。
对于 NGINX 配置:
来自同一域:
server {
listen 80;
server_name XXXXX.com;
#Django app served using Gunicorn
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 120;
proxy_read_timeout 120;
proxy_pass http://localhost:8000/;
}
#PHP processed by Apache
location ~ \.php$ {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 120;
proxy_read_timeout 120;
proxy_pass http://localhost:<port you configured apache to listen on>/;
}
}
来自子域:
#Server block for Django
server {
listen 80;
server_name XXXXX.com;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 120;
proxy_read_timeout 120;
proxy_pass http://localhost:8000/;
}
}
#Server block for PHP served using Apache with a subdomain
server {
listen 80;
server_name blog.XXXXX.com;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 120;
proxy_read_timeout 120;
proxy_pass http://localhost:<port you configured apache to listen on>/;
}
}
注意:您不需要 root 指令,因为您的应用程序由 Gunicorn 提供服务或移交给 Apache(您将在 VirtualHost 中定义根)
编辑配置文件后,重启 Apache,然后重启 Nginx,它应该可以工作。
关于python - 同时将 nginx 设置为 apache 和 gunicorn 的代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10533778/