我已經花了幾個小時在StackOverflow和其他教程,但我不明白爲什麼nginx
返回403 Forbidden
當我導航到localhost
。Django/gunicorn/nginx:403 Forbidden
這裏是我的gunicorn啓動腳本(位於應用程序根目錄):
#!/bin/bash
# http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/
NAME="mbta_django_gunicorn"
SOCKFILE=run/gunicorn.sock
USER=alexpetralia # the user to run as
GROUP=alexpetralia # the group to run as
NUM_WORKERS=5
DJANGO_SETTINGS_MODULE=mbta_django.settings
DJANGO_WSGI_MODULE=mbta_django.wsgi
echo "Starting $NAME"
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start Django Unicorn
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
# --bind=localhost:8000 \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=- \
--reload
這裏是我的nginx.conf:
user alexpetralia alexpetralia; # www-data
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
這裏是位於sites-available
在我的應用程序特定的nginxconf文件(並鏈接在sites-enabled
):
upstream mbta_django_server {
server unix:/home/alexpetralia/Projects/mbta_django/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
client_max_body_size 4G;
keepalive_timeout 5;
root /home/alexpetralia/Projects/mbta_django/static/;
location /static/ {
autoindex on;
alias /home/alexpetralia/Projects/mbta_django/static/;
}
}
我的主管設置f或運行gunicorn(更改PATH
才能使用的virtualenv):
[program:mbta_gunicorn]
command=/home/alexpetralia/Projects/mbta_django/gunicorn_ctl
stdout_logfile=/home/alexpetralia/Projects/mbta_django/logs/mbta_gunicorn.log
stderr_logfile=/home/alexpetralia/Projects/mbta_django/logs/mbta_gunicorn.log
redirect_stderr=true
autorestart=true
stopsignal=KILL
killasgroup=true
stopasgroup=true
environment=PATH="/home/alexpetralia/Projects/mbta_django/venv/bin"
directory=/home/alexpetralia/Projects/mbta_django
這強烈感覺就像一個權限問題,但我已經在我的web應用程序的根文件夾使用chmod -R 775 mbta_django
。我很猶豫chown
吧。我不明白爲什麼,如果gunicorn加載了正確的用戶,就像nginx一樣,那麼應該不會有權限問題。
也許這與gunicorn有關,而不是nginx?我發現如果gunicorn正在運行,我可以訪問我的應用程序(沒有靜態文件),即使它綁定到Unix套接字而不是127.0.0.1:8000,也很奇怪。
謝謝。
UPDATE
Nginx的錯誤日誌(樣本,這是非常簡單,只是這一點):
2016/01/18 16:42:40 [error] 20773#0: *5 directory index of "/home/alexpetralia/Projects/mbta_django/static/" is forbidden, client: 127.0.0.1, server: , request: "GET/HTTP/1.1", host: "localhost"
2016/01/18 16:42:40 [error] 20773#0: *5 directory index of "/home/alexpetralia/Projects/mbta_django/static/" is forbidden, client: 127.0.0.1, server: , request: "GET/HTTP/1.1", host: "localhost"
Gunicorn錯誤日誌(樣本,該目錄名稱的部分是來自frmo教程here命令) :
Starting mbta_django_gunicorn
/home/alexpetralia/Projects/mbta_django/gunicorn_ctl: line 20: dirname: command not found
[2016-01-18 18:03:08 +0000] [1996] [INFO] Starting gunicorn 19.4.5
[2016-01-18 18:03:08 +0000] [1996] [INFO] Listening at: http://127.0.0.1:8000 (1996)
[2016-01-18 18:03:08 +0000] [1996] [INFO] Using worker: sync
[2016-01-18 18:03:08 +0000] [2008] [INFO] Booting worker with pid: 2008
[2016-01-18 18:03:08 +0000] [2009] [INFO] Booting worker with pid: 2009
[2016-01-18 18:03:08 +0000] [2016] [INFO] Booting worker with pid: 2016
[2016-01-18 18:03:08 +0000] [2019] [INFO] Booting worker with pid: 2019
[2016-01-18 18:03:08 +0000] [2022] [INFO] Booting worker with pid: 2022
所有權靜態文件夾:
[email protected]:~$ namei -ov /home/alexpetralia/Projects/mbta_django/static
f: /home/alexpetralia/Projects/mbta_django/static
d root root /
d root root home
d alexpetralia alexpetralia alexpetralia
d alexpetralia alexpetralia Projects
d alexpetralia alexpetralia mbta_django
d alexpetralia alexpetralia static
權限靜態文件夾:
drwxr-xr-x 6 alexpetralia alexpetralia 4096 Jan 8 12:43 static
Gunicorn過程:
[email protected]:~/Projects/mbta_django$ ps aux | grep gunicorn
root 1942 0.0 0.4 57416 15972 ? S 18:52 0:00 /home/alexpetralia/Projects/mbta_django/venv/bin/python /home/alexpetralia/Projects/mbta_django/venv/bin/gunicorn mbta_django.wsgi:application --name mbta_django_gunicorn --workers 5 --user=alexpetralia --group=alexpetralia
alexpet+ 1951 0.0 0.0 ? S 18:52 0:00 /home/alexpetralia/Projects/mbta_django/venv/bin/python /home/alexpetralia/Projects/mbta_django/venv/bin/gunicorn mbta_django.wsgi:application --name mbta_django_gunicorn --workers 5 --user=alexpetralia --group=alexpetralia
alexpet+ 1954 0.0 0.0 ? S 18:52 0:00 /home/alexpetralia/Projects/mbta_django/venv/bin/python /home/alexpetralia/Projects/mbta_django/venv/bin/gunicorn mbta_django.wsgi:application --name mbta_django_gunicorn --workers 5 --user=alexpetralia --group=alexpetralia
alexpet+ 1957 0.2 1.6 226280 63612 ? S 18:52 0:01 /home/alexpetralia/Projects/mbta_django/venv/bin/python /home/alexpetralia/Projects/mbta_django/venv/bin/gunicorn mbta_django.wsgi:application --name mbta_django_gunicorn --workers 5 --user=alexpetralia --group=alexpetralia
alexpet+ 1964 0.1 0.0 ? S 18:52 0:00 /home/alexpetralia/Projects/mbta_django/venv/bin/python /home/alexpetralia/Projects/mbta_django/venv/bin/gunicorn mbta_django.wsgi:application --name mbta_django_gunicorn --workers 5 --user=alexpetralia --group=alexpetralia
alexpet+ 1975 0.0 0.8 ? S 18:52 0:00 /home/alexpetralia/Projects/mbta_django/venv/bin/python /home/alexpetralia/Projects/mbta_django/venv/bin/gunicorn mbta_django.wsgi:application --name mbta_django_gunicorn --workers 5 --user=alexpetralia --group=alexpetralia
Nginx的過程:
[email protected]:~/Projects/mbta_django$ ps aux | grep nginx
root 1362 0.0 0.0 85892 2712 ? Ss 18:52 0:00 nginx: master process /usr/sbin/nginx
alexpet+ 1363 0.0 0.0 86172 3404 ? S 18:52 0:00 nginx: worker process
alexpet+ 1364 0.0 0.0 86172 3404 ? S 18:52 0:00 nginx: worker process
alexpet+ 1365 0.0 0.0 86172 3404 ? S 18:52 0:00 nginx: worker process
alexpet+ 1366 0.0 0.0 86172 3404 ? S 18:52 0:00 nginx: worker process
你的nginx和gunicorn日誌說什麼? '/ var/log/nginx/nginx_error.log'和'/ home/alexpetralia/Projects/mbta_django/logs/mbta_gunicorn.log' – YPCrumble
@YPCrumble更新錯誤日誌 –
'/ home/alexpetralia/Projects/mbta_django/static /' – YPCrumble