나는 주제에 대해 광범위한 검색을 수행했지만 아무것도 도움이 될 수 없었습니다.
저는 현재 Django 사이트를 구축하고 있으며 Nginx를 사용하여 Gunicorn에 대한 역방향 프록시를 사용하고 있습니다.
내 앱을 하위 도메인으로 분리하려고 합니다. (blog.example.com, admin.example.com, user.example.com 등)
내 초기 프로젝트가 Nginx만 사용했기 때문에 DNS 레코드를 관리했고 그에 따라 작동하고 있습니다.
"하위 폴더" example.com/blog로 이동하여 앱에 액세스할 수 있습니다.
구성은 다음과 같습니다.
/etc/systemd/system/gunicorn.socket:
[Unit]
Description=Gunicorn socket for example.com
[Socket]
ListenStream=/run/example.sock
[Install]
WantedBy=sockets.target
/etc/systemd/system/gunicorn.service:
[Unit]
Description=gunicorn daemon for example.com
Requires=gunicorn.socket
After=network.target
[Service]
User=example
Group=www-data
WorkingDirectory=/home/example/project
ExecStart=/home/sammy/project/projectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
project.wsgi:application
[Install]
WantedBy=multi-user.target
/etc/nginx/sites-사용 가능/예:
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/example/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
blog.example.com에 대한 또 다른 서버 블록을 추가하고 Proxy_pass 행에 /blog를 추가했습니다. proxy_pass http://unix:/run/gunicorn.sock/blog;
앱 폴더로 리디렉션되기를 바랍니다.
저는 소켓 방법을 처음 사용하는데 리디렉션을 수행하는 방법을 모릅니다.
Nginx가 리디렉션과 모든 것을 처리해야 한다는 것을 알고 있지만 더 이상 진행할 수는 없습니다.
어떤 도움이라도 대단히 감사하겠습니다.
답변1
앱당 하나의 gunicorn 서비스를 가질 수 있습니다.
/etc/systemd/system/blog.example.com.service
[Unit]
Description=gunicorn daemon for blog.example.com
Requires=gunicorn.socket
After=network.target
[Service]
User=example
Group=www-data
WorkingDirectory=/home/example/project/blog
ExecStart=/home/sammy/project/projectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/blog.example.com.sock \
project.wsgi:application
[Install]
WantedBy=multi-user.target
/etc/systemd/system/admin.example.com.service
[Unit]
Description=gunicorn daemon for admin.example.com
Requires=gunicorn.socket
After=network.target
[Service]
User=example
Group=www-data
WorkingDirectory=/home/example/project/admin
ExecStart=/home/sammy/project/projectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/admin.example.com.sock \
project.wsgi:application
[Install]
WantedBy=multi-user.target
그런 다음 nginx에 해당 .sock
파일을 지정합니다.
/etc/nginx/sites-available/blog.example.com
server {
listen 80;
server_name blog.example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/example/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/blog.example.com.sock;
}
}
/etc/nginx/sites-available/admin.example.com
server {
listen 80;
server_name admin.example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/example/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/admin.example.com.sock;
}
}
답변2
당신은 사용해야합니다별도의 서비스 및 소켓 파일모든 앱과 용도에 대해유닉스 소켓 대신 명시적 호스트.
당신의 unix:/run/blog
것과 같은 용어를 변경하십시오127.0.0.1:8000
소켓그리고서비스파일. WorkingDirectory
매개변수는 모든 서비스 파일에서 동일해야 하며, 포함된 디렉터리를 가리켜야 합니다 manage.py
. 마지막 proxy_pass http://127.0.0.1:8000/blog/;
으로nginx구성.
/etc/systemd/system/blog.example.com.socket
Unit]
Description=Gunicorn socket for blog.example.comg
[Socket]
ListenStream=8000
[Install]
WantedBy=sockets.target
/etc/systemd/system/blog.example.com.service
[Unit]
Description=gunicorn daemon for blog.example.com
Requires=blog.example.com.socket
After=network.target
[Service]
User=example
Group=www-data
WorkingDirectory=/home/example/project/blog
ExecStart=/home/sammy/project/projectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind 127.0.0.1:8000 \
project.wsgi:application
[Install]
WantedBy=multi-user.target
/etc/nginx/sites-available/blog-example:
server {
listen 80;
server_name blog.example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/example/project;
}
location / {
include proxy_params;
proxy_pass http://127.0.0.1:9000/blog/;
}
}