我對該主題進行了廣泛的搜索,但沒有任何幫助。
我目前正在建立一個 Django 站點,並使用 Nginx 反向代理到 Gunicorn。
我正在嘗試將我的應用程式分成子網域。 (blog.example.com、admin.example.com、user.example.com 等)
我負責處理 DNS 記錄,它們正在相應地工作,因為我最初的專案僅使用 Nginx。
我可以透過導航到「子資料夾」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-available/範例:
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 套接字。
更改條款,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/;
}
}