set nginx as proxy for both apache and gunicorn at the same time

I have all my code in django, hence using gunicorn to host django, and using nginx as reverse proxy. I now want to host some PHP files on the same domain (or subdomain) using an nginx server as reverse proxy, and apache server.

Is it possible to reverse proxy nginx for 2 different servers? (if gunicorn fails, try apache)

i can directly host the php files on apache, but port 80 is taken by nginx. Lets say I want to host a php based Blog application - wordpress on blog.XXXX.com using apache ? and my regular website on XXXX.com using nginx and gunicorn

here is the relevant part of my current 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/;
        }
   }

Yes, this is possible. You'll need to configure Apache to listen on a different port though.

Don't select a reserved port and ensure that it's open (and that you can access it. Unix has a port range, usually defined in /proc/sys/net/ipv4/ip_local_port_range if you chose ipv4, which you probably have judging by your Nginx config)

For Apache configuration:

Edit you apache main configuration file and set the Listen directive as follows:

Listen 127.0.0.1:<port number that's open> //Use 8080 as port for a start

Then create a VirtualHost as follows:

<VirtualHost 127.0.0.1:<port number defined above>>
    config goes here
</VirtualHost>

You can do this in 2 ways, one from the same domain and one from a subdomain.

For NGINX configuration:

From the same domain:

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>/;
    }
}

From a subdomain:

#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>/;
    }
}

Note: You don't need the root directive because your applications are being served by either Gunicorn or being handed over to Apache (where you'll define a root in the VirtualHost)

Once you've edited your config files, restart Apache and then restart Nginx, it should work.

链接地址: http://www.djcxy.com/p/52548.html

上一篇: 什么是公正的

下一篇: 同时将nginx设置为apache和gunicorn的代理