arsaevm
By:
arsaevm

PHP and Django on nginx same time. Problem!

January 4, 2017 597 views
Django Nginx PHP Ubuntu

So, i have a html landing page with some php code and django project.
Want to make like this:
site.com = landing page **
site.com/dashboard = **django project
.

what i´ve already did?

create simple index.html in /var/www/html

changed /etc/nginx/sites-available/default


server {
    listen 8001 default_server;
    listen [::]:8001 default_server ipv6only=on;
    server_name localhost;

    location / {
            proxy_pass http://127.0.0.1:8001;
            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;
            proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ~ /lp/.*\.php$ {
            root /var/www/html;
            index index.php index.html index.htm;
            set $php_root /var/www/html/;

            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location /lp {
            root /var/www/html;
            index index.php index.html index.htm;
            set $php_root /var/www/html/;

            try_files $uri $uri/ /index.php;
    }
}

Thanks for any help

2 comments
  • It looks like you've got things configured to have your django site at / while your landing page is at/lp` switching this around should work better. Can you provide some more information on what you've tried so far and what results you've seen?

  • have treid your suggestion, doesn´t work for me.
    may be will be easer with too domains, site.com and sub.site.com ?

1 Answer

@arsaevm

Looking at the configuration you provided, you're setting the server block to listen in on Port 8001 and then proxying all requests to / to port 8001 which is redundant.

Breakdown

The second line reading:

listen 8001 default_server;

says that:

server_name localhost;

should be listening to port 8001 for connection attempts, while

proxy_pass http://127.0.0.1:8001;

says that all connections to / should be proxied to port 8001 even though requests to port 8001 should already be getting handled.

What Changes Are Needed?

You really only need to proxy requests to your Django application since it's what should be running on another port.

Your original post says that you want site.com to be the landing page and site.com/dashboard to be where requests for the Django project are handled, correct? If so, let's get started :-).

We'll be starting with a blank canvas and building it up, so here's our blank canvas:

server {
}

Let's start by listening in on Port 80, the standard HTTP port.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
}

Change server_name localhost; to use your actual domain name; if your domain is example.com, then:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name example.com www.example.com;
}

Directly below server_name, define your root path using:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name example.com www.example.com;
    root /var/www/html;
}

Now we need to setup our base location block, i.e. /.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name example.com www.example.com;
    root /var/www/html;

    location / {
        try_files $uri $uri/ /index.php;
    }
}

Now, from your first post, I'm not sure if you're wanting PHP to only be handled from specific location such as /lp/ or if you want PHP to be served from any location that isn't /dashboard. For the purpose of getting you setup, I'm going to run with running PHP anywhere a valid request is made.

So we need to setup our PHP block. What this does is simply take care of requests for PHP files. You need to make sure that your PHP-FPM configuration is using sockets instead of TCP though (as long as you've not modified the configuration, sockets are the defaults).

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name example.com www.example.com;
    root /var/www/html;

    location / {
        try_files $uri $uri/ /index.php;
    }

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

Now we need to handle requests to /dashboard, which is where you've said your Django app will reside. Using the default port of 8001, this is where we proxy requests to the port Django is listening on.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name example.com www.example.com;
    root /var/www/html;

    location / {
        try_files $uri $uri/ /index.php;
    }

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

    location /dashboard {
        proxy_pass http://127.0.0.1:8001;
        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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Hopefully that'll help get you up and running.

Have another answer? Share your knowledge.