且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在使用同一个域的NGINX服务器上运行django和wordpress?

更新时间:2023-11-22 18:19:28

感谢 alex ,以帮助我解决这个问题。



这是解决方案



Django应用程序目录 - / home / ubuntu / django



Wordpress目录 - / var / www / html / blog



NGINX Conf文件

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

位置/ {
proxy_pass http://127.0.0.1:8000;
proxy_redirect关闭;
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;
}

位置〜/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;
包含fastcgi_params;
}

位置/博客{
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 /;
}

}

注 - strong>请用 http://example.com/blog 替换您的home和siteurl 在wordpress的wp-location表中



Django应用程序在example.com上运行



博客运行的例子。 com / blog


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 ?

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

Django app running on example.com

Blog running on example.com/blog