【问题标题】:How to run django and wordpress on NGINX server using same domain?如何使用同一域在 NGINX 服务器上运行 django 和 wordpress?
【发布时间】:2017-03-27 15:32:42
【问题描述】:

我尝试了很多方法,但不知道如何运行 Django on example.comwordpress on example.com/blog

以下为 Django 和 Wordpress 运行的项目目录结构。

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

Django 应用在 - example.com:8000 上成功运行

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

Wordpress 在 - example.com 上成功运行

Nginx 配置

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

注意- 由 gunicorn 运行的 Django 应用程序,我知道子域可能是解决方案,但我不希望这样。

如何为 Wordpress 和 Django 编写 nginx 配置以在 example.com 上运行 Django app 和在 example.com/blog 上运行 Wordpress

【问题讨论】:

  • 尝试子域wordpress.example.comblog.example.com - 在一个文件中server_name wordpress.example.com; 和其他server_name blog.example.com;
  • PHP 需要location /blog 吗?
  • 我的 Gunicorn 配置适用于 localhost:3001 - 你必须更改端口(和server_name)。 pastebin.com/4cc2vEKj

标签: php python django wordpress nginx


【解决方案1】:

只需将这些行添加到您的配置文件中。

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

【讨论】:

    【解决方案2】:

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

    解决办法

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

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

    NGINX 配置文件

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

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

    现在您的 Django 应用在 example.com 上运行

    现在您的博客在 example.com/blog 上运行

    【讨论】:

      【解决方案3】:

      据我了解,您有一台运行 django 站点的服务器和一台运行您的 wordpress 站点的服务器。如果是这样,您可以这样做:

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

      如果您需要在中间使用 ngix 没有 apache 的 php 博客服务器,请使用类似:

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

      【讨论】:

      • 如果我对 django 使用proxy_pass http://127.0.0.1:8000;,对 wordpress 使用proxy_pass http://127.0.0.1;,那么 example.com 会打开 Django,但 example.com/blog/ 说页面未找到来自 django 端的错误?
      • 抱歉 example.com/blog/ 说 502 Bad Gateway,我认为它没有提供 php 文件
      • 502 Bad Gateway 表示被反向代理的服务器没有响应,您是在 apache 上运行博客吗?
      • 我注意到的一件事是,它看起来就像您尝试使用端口 80 来运行 apache 和 nginx,如果是这样,请将 apache 更改为其他端口。
      • 它认为我终于明白了你想要做的事情:你要在 nginx 上为 php 博客服务,中间没有 apache 对吗?
      猜你喜欢
      • 2017-08-21
      • 2016-02-29
      • 1970-01-01
      • 2017-04-23
      • 2016-12-17
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多