Nginx服务器下如何修改域名站点根目录至指定Django项目路径?
问题描述
我已将DNS指向我的域名,希望加载已安装的Django网站。当前访问www.mywebsite.com时,站点内容来自/var/www/html目录,但我希望改为加载/home/myname/kkappDashboard路径下的内容;我使用Nginx服务器,以下是我的配置文件:
当前sites-available配置
server { listen 80; server_name 151.236.222.57 www.mywebsite.com mywebsite.com; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/myname/kkappDashboard; } location / { include proxy_params; proxy_pass http://unix:/home/myname/kkappDashboard/kkappDashboard.sock; } } server { listen 443 ssl http2; server_name www.mywebsite.com; # redirect return 301 https://mywebsite.com.com$request_uri; } server { listen 443 ssl http2; server_name mywebsite.com; }
当前nginx.conf配置
## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; server { listen 80; server_name 151.236.222.xx; return 301 $https://www.mywebsite.com$request_uri; }
修复方案
禁用默认站点配置
默认的default站点配置(通常在/etc/nginx/sites-enabled/default)会指向/var/www/html,需要禁用它:sudo unlink /etc/nginx/sites-enabled/default启用自定义站点配置
确保你的sites-available配置文件已链接到sites-enabled目录:sudo ln -s /etc/nginx/sites-available/你的配置文件名 /etc/nginx/sites-enabled/修正nginx.conf语法错误
原return指令存在变量错误,正确写法如下:server { listen 80; server_name 151.236.222.xx; return 301 https://www.mywebsite.com$request_uri; }完善HTTPS配置
当前443端口的server块缺少SSL证书配置和代理规则,补充后如下(需替换为你的实际证书路径):server { listen 443 ssl http2; server_name www.mywebsite.com; # 修正原配置中的域名错误(mywebsite.com.com) return 301 https://mywebsite.com$request_uri; } server { listen 443 ssl http2; server_name mywebsite.com; # SSL证书路径 ssl_certificate /path/to/your/fullchain.pem; ssl_certificate_key /path/to/your/privkey.pem; # 静态文件与favicon配置 location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/myname/kkappDashboard; } # 代理到Django Unix Socket location / { include proxy_params; proxy_pass http://unix:/home/myname/kkappDashboard/kkappDashboard.sock; } }验证配置并重启Nginx
先检查配置语法是否合法:sudo nginx -t确认无误后重启Nginx:
sudo systemctl restart nginx检查Django Socket权限
确保Nginx能访问Django的Socket文件:# 查看Socket文件状态 ls -l /home/myname/kkappDashboard/kkappDashboard.sock # 调整权限(若Nginx无访问权限) sudo chown www-data:www-data /home/myname/kkappDashboard/kkappDashboard.sock
内容的提问来源于stack exchange,提问作者JohnnyQ
相关产品推荐
相关产品推荐

