Question

PHP and Django on nginx same time. Problem!

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

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@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.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel