配置 nginx 以与 django 和 moodle 配合使用

配置 nginx 以与 django 和 moodle 配合使用

我正在尝试配置 nginx 以并行运行 moodle 网站、django 网站和 jellyfin 网站。我的域名是www.example.com,我想将以 结尾的所有内容设置为www.example.com/moodlemoodle 网站,并将所有非 moodle 页面设置为由 django 和 jellyfin 处理。

以下nginx配置适用于 django 和 jellyfin:

upstream champhy {
    server unix:/mnt/data1/django_apps/champhy/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80 default_server;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    server_name www.example.com; # managed by Certbot

    location /jellyfin {
            return 302 $scheme://$host/jellyfin/;
    }
    location /jellyfin/ {
            proxy_pass http://localhost:8096/jellyfin/;
            proxy_pass_request_headers on;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Host $http_host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $http_connection;
            proxy_buffering off;
    }

    location /media  {
            alias /mnt/data1/data; 
            client_max_body_size 50m;
    }
    location /static {
            client_max_body_size 50m;
            alias /mnt/data1/django_static/champhy;
    }

    location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header Host $http_host;
            proxy_redirect off;
    }
}

我读了很多关于 moodle 的文档,但我不知道如何让它工作。我对 php 很陌生,根据我尝试添加如下新位置:

location / {
    try_files $uri $uri/ =404;        
}

location /dataroot/ {
    internal;
    alias /var/www/html/moodledata/;
}

location ~ /moodle/(.\+.php(/|$) {
    fastcgi_split_path_info  ^moodle/(.+\.php)(/.+)$;
    fastcgi_index            index.php;
    fastcgi_pass             unix:/var/run/php/php7.3-fpm.sock;
    include                  fastcgi_params;
    fastcgi_param   PATH_INFO       $fastcgi_path_info;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

但经过大量努力,我还是失败了。请注意,当我禁用 django 和 jellyfin 并且只运行 moodle 时www.example.com,它​​就可以正常工作了。请帮帮我,我是一名老师,想在即将到来的封锁之前为他的学生做好准备……谢谢

答案1

感谢这里得到的一点帮助,我可以解决我的问题...这是我当前的 nginx 配置:

upstream champhy {
    server unix:/mnt/data1/django_apps/champhy/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80 default_server;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl; # managed by Certbot

    ssl_certificate /etc/letsencrypt/live/www.example.ch/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.example.ch/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    server_name www.example.com; # managed by Certbot

    # jellyfin
    location /jellyfin {
        return 302 $scheme://$host/jellyfin/;
    }
    location /jellyfin/ {
        proxy_pass http://localhost:8096/jellyfin/;
        proxy_pass_request_headers on;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
        proxy_buffering off;
    }

    # server authenticated medias for tepepe
    location /protected_media/ {
        client_max_body_size 50m;
        add_header X-Accel-Buffering yes;
        add_header X-Accel-Limit-Rate off;
        alias      /mnt/data1/data/tepepe/protected_media/;
        expires 24h;
        internal;
    }

    #champhy media
    location /media  {
        alias /mnt/data1/data; 
        client_max_body_size 50m;
    }
    #champhy static
    location /static {
        client_max_body_size 50m;
        alias /mnt/data1/django_static/champhy; 
    }

    root /var/www/html/;
    location /dataroot/ {
        internal;
        alias /mnt/data1/moodle-data/;
    }

    location ~ /moodle/(.+\.php)?(/|$)? {
        fastcgi_split_path_info  ^(.+\.php)(/?.*)$;
        fastcgi_pass             unix:/var/run/php/php7.3-fpm.sock;
        include                  /etc/nginx/fastcgi_params;
        fastcgi_index           index.php;
        fastcgi_param   PATH_INFO       $fastcgi_path_info;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://champhy;
            break;
        }
    }
}

相关内容