728x90
반응형

Django 프로젝트의 settings.py에 다음을 추가한다.

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

그리고 다음 명령어를 이용하여 Django에 내장된 static 파일을 지정한 디렉토리에 저장한다.

$ python manage.py collectstatic

gunicorn 관련된 설정은 공식 문서와 예제 파일을 참고하여 작성한다. 그리고 다음과 같은 명령어로 실행한다.

$ gunicorn -c gunicorn.conf.py {Django 프로젝트 이름}.wsgi &

nginx.conf에 다음과 같이 추가한다.

http {
 include /etc/nginx/mime.types;
 default_type application/octet-stream;

 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent "$http_referer" '
 '"$http_user_agent" "$http_x_forwarded_for"';

 access_log /var/log/nginx/access.log main;

 sendfile on;
 #tcp_nopush on;

 #keepalive_timeout 0;
 keepalive_timeout 65;

 #gzip on;

 # Load modular configuration files from the /etc/nginx/conf.d directory.
 # See http://nginx.org/en/docs/ngx_core_module.html#include
 # for more information.
 include /etc/nginx/conf.d/*.conf;

 index index.html index.htm;

 upstream app_server {
     server 127.0.0.1:8000;
     # For a TCP configuration:
     # server 192.168.0.7:8000 fail_timeout=0;
 }

 server {
     listen 80;
     server_name localhost;
     root /usr/share/nginx/html;

     #charset koi8-r;

     #access_log /var/log/nginx/host.access.log main;

     location /static {
         alias /path/static; # static 파일 위치
     }

     location / {
         # root html;
         # index index.html index.htm;
         try_files $uri @proxy_to_app;
     }

     location @proxy_to_app {
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_redirect off;
         proxy_pass http://app_server;
     }
}

위와 같이 수정한 설정 파일을 다시 읽는다.

$ sudo /etc/init.d/nginx reload

혹은

$ service nginx reload

static 파일을 제대로 불어오지 못하는 경우 nginx.conf에서 다음을 수정하면 된다.

# user nginx
# 기본으로 설정된 user가 nginx인 경우 root로 변경하거나
# static 파일 접근 권한이 있는 user로 변경한다.
user root

 

반응형

'Python' 카테고리의 다른 글

[Python] GeoDjango  (0) 2020.01.25
[Python] Django logging  (0) 2020.01.25
[Python] nginx + Django  (0) 2020.01.25
728x90
반응형

Django와 nginx를 연동하기 위해서 flup 라이브러리를 설치한다.

$ pip install flup

Django 실행은 다음과 같다.

$ python manage.py runfcgi method=prefork pidfile=PID_FILE host=127.0.0.1 port=8000

nginx.conf 에 다음과 같이 추가한다.

server {
    listen 80
    server_name localhost;

    location /static/ { #static url
        autoindex on;
        root /static/; #static 파일들이 저장된 디렉토리
    }
    location / { #사용할 url
        #root /usr/share/nginx/html;
        #index index.html index.htm;
        fastcgi_pass 127.0.0.1:8000;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_pass_header Authorization;
        fastcgi_intercept_errors off;
    }
}

static url을 설정할 때 /static/으로 해서 파일을 불러오지 못할 경우 앞이나 뒤의 /를 제거하면 된다.

 

위와 같이 수정한 설정 파일을 다시 읽는다.

$ sudo /etc/init.d/nginx reload

 

반응형

'Python' 카테고리의 다른 글

[Python] GeoDjango  (0) 2020.01.25
[Python] Django logging  (0) 2020.01.25
[Python] nginx + gunicorn + Django  (0) 2020.01.25

+ Recent posts