By arsaevm
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
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!
Accepted Answer
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.
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.
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.