nginx 使用子文件夹和“A 记录”

nginx 使用子文件夹和“A 记录”

我真的被困在这里了,在相关文章中找不到帮助,这是我第一次处理 nginx。我的 Synology NAS 上有一个使用 nginx 运行的“Moodle”安装,我想通过域名从外部访问它,即我不想使用我的公共 IP。我已经有一个域名(假设为 mymoodle.com),并向该域名添加了指向我路由器公共 IP 的“A 记录”。当我在浏览器中输入“mymoodle.com”时,它会将我指向 Synology Web 文件夹的根目录,在我的情况下,该文件夹位于此处:

/var/services/web/

并包含以下内容:

index.html  moodle  phpMyAdmin  web_images

我的浏览器基本上会呈现 index.html,所以我的 A 记录似乎按预期工作,即 mymoodle.com 指向该文件夹中的 index.html。但我希望 mymoodle.com 指向 moodle 子文件夹(其中包含 index.php 文件)。

我找到了 Moodle 的以下 nginx 配置文件:

/etc/nginx/conf.d/www.Moodle.conf

看起来像:

location ^~ /moodle {
    include proxy.conf;
    proxy_read_timeout 3600s;
    proxy_pass  http://127.0.0.1:914;
}

我尝试了很多设置,但似乎都不起作用。我最后一次尝试如下:

server {
   server_name moodle.trucker.in;
   root /var/services/web/moodle;
}

但当然没有成功。/etc/nginx/ 的内容如下所示

ash-4.3# ls -l /etc/nginx/
total 52
lrwxrwxrwx 1 root root   20 Oct  9 12:13 app.d -> /var/tmp/nginx/app.d
lrwxrwxrwx 1 root root   27 Oct  9 12:13 conf.d -> /usr/local/etc/nginx/conf.d
-rw-r--r-- 1 root root 1077 Jul 13 00:42 fastcgi.conf
-rw-r--r-- 1 root root 1007 Jul 13 00:42 fastcgi_params
-rw-r--r-- 1 root root 2837 Jul 13 00:42 koi-utf
-rw-r--r-- 1 root root 2223 Jul 13 00:42 koi-win
-rw-r--r-- 1 root root 3957 Jul 13 00:42 mime.types
-rw-r--r-- 1 root root 9043 Oct 25 21:13 nginx.conf
-rw-r--r-- 1 root root 3491 Jul 13 00:31 nginx.conf.default
-rw-r--r-- 1 root root  277 Jul 13 00:31 proxy.conf
-rw-r--r-- 1 root root 1204 Jul 13 00:42 scgi_params
lrwxrwxrwx 1 root root   34 Oct  9 12:13 sites-enabled -> /usr/local/etc/nginx/sites-enabled
-rw-r--r-- 1 root root  664 Jul 13 00:42 uwsgi_params
-rw-r--r-- 1 root root 3610 Jul 13 00:42 win-utf

以下是所要求的 nginx.conf 文件:

worker_processes        auto;
#worker_cpu_affinity    auto;
worker_rlimit_nofile    65535;

include conf.d/main.conf;

events {
    use             epoll;
    multi_accept    on;
    accept_mutex    on;
    worker_connections 1024;

    include conf.d/events.conf;
}

http {
    include         mime.types;
    default_type    application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  off;
    #access_log syslog:server=unix:/dev/log,facility=local7,tag=nginx_access,nohostname main;
    error_log   syslog:server=unix:/dev/log,facility=local7,tag=nginx_error,nohostname error;

    sendfile        on;
    server_tokens   off;

    proxy_request_buffering     off;
    fastcgi_request_buffering   off;
    scgi_request_buffering      off;

    proxy_buffering     off;
    fastcgi_buffering   off;
    scgi_buffering      off;

    resolver_timeout              5s;
    client_header_timeout         10s;
    client_body_timeout           60s;
    send_timeout                  60s;
    keepalive_timeout             65s 20s;
    client_max_body_size          0;
    server_names_hash_max_size    8192;

    ssl_certificate           /usr/syno/etc/certificate/system/default/fullchain.pem;
    ssl_certificate_key       /usr/syno/etc/certificate/system/default/privkey.pem;
    ssl_protocols             TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers               ECDHE-....;
    ssl_dhparam               /usr/syno/etc/ssl/dh2048.pem;
    ssl_prefer_server_ciphers on;

    ssl_session_tickets       off;
    ssl_session_cache         shared:SSL:1m;
    ssl_session_timeout       3600s;

    server_tag     "nginx";

    gzip_disable    "msie6";
    gzip_min_length 1000;
    gzip_types      text/plain text/css application/javascript application/json;
    gzip_vary       on;
    gzip_static     on;

    upstream synoscgi {
        server unix:/run/synoscgi.sock;
    }

    index index.html index.htm index.php;

    set_real_ip_from 127.0.0.1;
    real_ip_header X-Real-IP;

    server {
        listen 5000 default_server;
        listen [::]:5000 default_server;

        server_name _;

        gzip on;

        include app.d/alias.*.conf;
        root /usr/syno/synoman;
        index index.cgi;

        ignore_invalid_headers off;

        include app.d/dsm.*.conf;
        include /usr/syno/share/nginx/conf.d/dsm.*.conf;
        include conf.d/dsm.*.conf;

        location = / {
            try_files $uri /index.cgi$is_args$query_string;
        }

        location ~ ^/volume(?:X|USB|SATA|Gluster)?\d+/ {
            internal;

            root /;

            include app.d/x-accel.*.conf;
            include conf.d/x-accel.*.conf;
        }

        location ~ /webman/modules/(PersonalSettings|ExternalDevices|FileBrowser)/index_ds.php$ {
            alias /usr/syno/share/OAuth/index_ds.php;
            default_type text/html;
        }

        location ~ \.cgi {
            include             scgi_params;
            scgi_read_timeout   3600s;
            scgi_pass           synoscgi;
        }

        error_page 403 404 500 502 503 504 @error_page;

        location @error_page {
            root /usr/syno/share/nginx;
            rewrite (.*) /error.html break;
        }

        location ~ ^/webman/modules/Indexer/ {
            deny all;
        }

        location ~ ^/webapi/lib/ {
            deny all;
        }

        location ~ ^/webapi/(:?(:?.*)\.lib|(:?.*)\.api|(:?.*)\.auth|lib.def)$ {
            deny all;
        }

        location ~ /\. { access_log off; log_not_found off; deny all; }

        location ~* \.(?:js|css|png|jpg|gif|ico)$ {
            access_log off;
            log_not_found off;
        }

        location = /favicon.ico {
            access_log off;
            log_not_found off;
        }

        location = /robots.txt {
            allow all;
            access_log off;
            log_not_found off;
        }
    }

    server {
        listen 5001 default_server ssl;
        listen [::]:5001 default_server ssl;

        server_name _;

        include app.d/alias.*.conf;
        root /usr/syno/synoman;
        index index.cgi;

        ignore_invalid_headers off;

        include app.d/dsm.*.conf;
        include /usr/syno/share/nginx/conf.d/dsm.*.conf;
        include conf.d/dsm.*.conf;

        location = / {
            try_files $uri /index.cgi$is_args$query_string;
        }

        location ~ ^/volume(?:X|USB|SATA|Gluster)?\d+/ {
            internal;

            root /;

            include app.d/x-accel.*.conf;
            include conf.d/x-accel.*.conf;
        }

        location ~ /webman/modules/(PersonalSettings|ExternalDevices|FileBrowser)/index_ds.php$ {
            alias /usr/syno/share/OAuth/index_ds.php;
            default_type text/html;
        }


        location ~ \.cgi {
            include             scgi_params;
            scgi_read_timeout   3600s;
            scgi_pass           synoscgi;
        }

        error_page 403 404 500 502 503 504 @error_page;

        location @error_page {
            root /usr/syno/share/nginx;
            rewrite (.*) /error.html break;
        }

        location ~ ^/webman/modules/Indexer/ {
            deny all;
        }

        location ~ ^/webapi/lib/ {
            deny all;
        }

        location ~ ^/webapi/(:?(:?.*)\.lib|(:?.*)\.api|(:?.*)\.auth|lib.def)$ {
            deny all;
        }

        location ~ /\. { access_log off; log_not_found off; deny all; }

        location ~* \.(?:js|css|png|jpg|gif|ico)$ {
            access_log off;
            log_not_found off;
        }

        location = /favicon.ico {
            access_log off;
            log_not_found off;
        }

        location = /robots.txt {
            allow all;
            access_log off;
            log_not_found off;
        }

    }

    server {
        listen 80 default_server;
        listen [::]:80 default_server;

        gzip on;

        server_name _;

        location ~ ^/volume(?:X|USB|SATA|Gluster)?\d+/ {
            internal;

            root /;

            include app.d/x-accel.*.conf;
            include conf.d/x-accel.*.conf;
        }

        include app.d/www.*.conf;
        include app.d/alias.*.conf;
        include /usr/syno/share/nginx/conf.d/www.*.conf;
        include conf.d/www.*.conf;

        location = /webdefault/images/logo.jpg {
            alias /usr/syno/share/nginx/logo.jpg;
        }

        error_page 403 404 500 502 503 504 @error_page;

        location @error_page {
            root /usr/syno/share/nginx;
            rewrite (.*) /error.html break;
        }

        location ^~ /.well-known/acme-challenge {
            root /var/lib/letsencrypt;
            default_type text/plain;
        }

        include app.d/.location.webstation.conf*;

        location / {
            rewrite ^ / redirect;
        }
        location ~ ^/$ {
            rewrite / http://$host:5000/ redirect;
        }
    }

    server {
        listen 443 default_server ssl;
        listen [::]:443 default_server ssl;

        server_name _;

        location ~ ^/volume(?:X|USB|SATA|Gluster)?\d+/ {
            internal;

            root /;

            include app.d/x-accel.*.conf;
            include conf.d/x-accel.*.conf;
        }

        include app.d/www.*.conf;
        include app.d/alias.*.conf;
        include /usr/syno/share/nginx/conf.d/www.*.conf;
        include conf.d/www.*.conf;

        location = /webdefault/images/logo.jpg {
            alias /usr/syno/share/nginx/logo.jpg;
        }

        error_page 403 404 500 502 503 504 @error_page;

        location @error_page {
            root /usr/syno/share/nginx;
            rewrite (.*) /error.html break;
        }

        location ^~ /.well-known/acme-challenge {
            root /var/lib/letsencrypt;
            default_type text/plain;
        }
        include app.d/.location.webstation.conf*;

        location / {
            rewrite ^ / redirect;
        }

        location ~ ^/$ {
            rewrite / https://$host:5001/ redirect;
        }
    }

    include conf.d/http.*.conf;
    include app.d/server.*.conf;
    include sites-enabled/*;
}

答案1

我无法保证以下步骤一定有效,因为整个设置过程相当复杂。

server {
    server_name moodle.tracker.im;
    listen 443 ssl;
    ssl_certificate /path/to/moodle/certificate;
    ssl_certificate_key /path/to/moodle/key;
    ... other SSL directives if desired...

    include proxy.conf;
    proxy_read_timeout 3600s;
    location / {
        proxy_pass  http://127.0.0.1:914;
    }
}

因此,需要为 Moodle 定义另一个虚拟主机,以便可以通过域名访问。您还需要确保已正确设置 Moodle 以使用该域名或根目录。

相关内容