7

I have tried many ways but do not know how to run Django on example.com and wordpress on example.com/blog

The following running project directory structure for Django and Wordpress.

Django app dir- /home/ubuntu/django

Django app running successfully on - example.com:8000

Wordpress dir - /var/www/html/blog

Wordpress running successfully on - example.com

Nginx configuration

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

    root /var/www/html/blog;
    index index.php index.html index.htm;

    server_name example.com;

    location / {
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/html;
    }

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

Note- Django app running by gunicorn, I know the subdomain may be the solution but I do not want that.

How to write nginx configuration for both Wordpress and Django to run Django app on example.com and Wordpress on example.com/blog ?

3
  • try subdomains wordpress.example.com, blog.example.com - in one file server_name wordpress.example.com; and in other server_name blog.example.com; Commented Nov 14, 2016 at 2:59
  • you need location /blog for PHP ? Commented Nov 14, 2016 at 3:03
  • my config for Gunicorn which works on localhost:3001 - you have to change port (and server_name). pastebin.com/4cc2vEKj Commented Nov 14, 2016 at 3:06

3 Answers 3

7

Thanks alex for helping me out to solve this problem.

Here is the solution

Django app dir- /home/ubuntu/django

Wordpress dir - /var/www/html/blog

NGINX Conf file

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

    location / {
            proxy_pass http://127.0.0.1:8000;
            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 ~ /blog/.*\.php$ {
            root /var/www/html;
            index index.php index.html index.htm;
            set $php_root /var/www/html/blog;

            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 /blog {
            root /var/www/html;
            index index.php index.html index.htm;
            set $php_root /var/www/html/blog;

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

location /static/ {
            alias /home/ubuntu/django/your_app_name/static/static_root/;
    }

    location /media/ {
        alias /home/ubuntu/django/your_app_name/media/ ;
    }

}

Note- please replace your home and siteurl with http://example.com/blog in wp-location table of wordpress

Now Your Django app running on example.com

Now Your Blog running on example.com/blog

Sign up to request clarification or add additional context in comments.

Comments

1

As i understand you have a server running your django site and one running your wordpress site. if so you can do something like this:

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

    access_log /var/log/nginx/example-access.log;

    location / {
        proxy_pass         http://127.0.0.1:3001;
        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 /blog/ {
        proxy_pass         http://127.0.0.1:(wordpress port);
        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;
    }
}

if you need to server the php blog from ngix no apache in the middle use somethon like:

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

    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
}

7 Comments

if i use proxy_pass http://127.0.0.1:8000; for django and proxy_pass http://127.0.0.1; for wordpress then example.com opens Django but example.com/blog/ says page not found error from django side?
Sorry example.com/blog/ says 502 Bad Gateway, I think it is not serving php files
502 Bad Gateway means the server that's back-proxied didn't respond, are you running your blog on apache?
One thing i noticed is that it seams like your trying to use port 80 to run both apache and nginx, if so change apache to other port.
it think i finally get what your trying to do: your going to server the php blog on nginx no apache in the middle right ?.
|
0

Just add these lines to your config file.

location ~ /blog/.*\.php$ {
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

location /blog {
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
    try_files $uri $uri/ /blog/index.php;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.