programing

같은 도메인을 사용하여 NGINX 서버에서 장고와 워드프레스를 실행하는 방법은 무엇입니까?

jooyons 2023. 7. 4. 21:53
반응형

같은 도메인을 사용하여 NGINX 서버에서 장고와 워드프레스를 실행하는 방법은 무엇입니까?

저는 많은 방법을 시도했지만 example.com 에서 장고를 실행하고 example.com/blog 에서 워드프레스를 실행하는 방법을 모릅니다.

Django 및 Wordpress에 대해 다음과 같은 실행 중인 프로젝트 디렉터리 구조입니다.

장고dir- /home/ubuntu/장고

장고 앱이 example.com:8000 에서 성공적으로 실행되고 있습니다.

워드프레스 dir - /var/www/html/blog

워드프레스가 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에서 실행되는 장고 앱, 나는 서브 도메인이 해결책일 수 있다는 것을 알지만 나는 그것을 원하지 않습니다.

example.com 에서 장고 실행하고 example.com/blog 에서 워드프레스를 실행하기 위해 워드프레스와 장고를 위한 nginx 구성을 작성하는 방법은 무엇입니까?

이 문제를 해결하는 데 도움을 주신 알렉스에게 감사드립니다.

여기 해결책이 있습니다.

장고dir- /home/ubuntu/장고

워드프레스 dir - /var/www/html/blog

NGINX Conf 파일

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

}

참고 - wp-location table of wordpress에서 당신의 집과 사이트 URL을 http://example.com/blog 으로 교체해주세요.

이제 example.com 에서 당신의 장고 앱이 실행됩니다.

이제 example.com/blog 에서 블로그를 실행합니다.

제가 알기로는 당신은 장고 사이트를 실행하는 서버와 워드프레스 사이트를 실행하는 서버를 가지고 있습니다.만약 그렇다면 당신은 다음과 같은 것을 할 수 있습니다.

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

이 행을 구성 파일에 추가하기만 하면 됩니다.

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

언급URL : https://stackoverflow.com/questions/40581246/how-to-run-django-and-wordpress-on-nginx-server-using-same-domain

반응형