首页 > 解决方案 > 如何更改 nginx 文档根目录以指向 ubuntu 20.10 中不同分区中的文件夹?

问题描述

要更改 nginx 文档根目录,我采取了以下步骤:

文件:/etc/nginx/sites-available/default

# root /var/www/html # commented this line
root /media/{user}/DATA/www # this change give " 404 not found " when i visit '127.0.0.1'
root /home/{user}/www # this works fine when i visit '127.0.0.1'

所以,当我将文档根目录更改为放置在不同分区中的文件夹时,它会给我“404 not found”

还有什么我必须改变的,将文档根指向放置在不同分区中的文件夹吗?

整个文件:/etc/nginx/sites-available/default

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

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    # root /var/www/html;
    root /home/sndp/www;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

编辑:

当我检查错误日志时出现错误:

2021/03/14 15:58:17 [crit] 24648#24648: *1 stat() "/media/sndp/DATA/www/" failed (13: Permission denied), client: ::1, server: _, request: "GET / HTTP/1.1", host: "localhost"

但两个目录的权限相同。

权限:

❯ ls -al /home/sndp/www/
drwxrwxr-x  5 sndp     sndp         4096 Mar 14 15:32 .
drwxr-xr-x 25 sndp     sndp         4096 Mar 13 12:39 ..
-rwxr-xr-x  1 www-data www-data       17 Mar 13 13:47 index.php

❯ ls -al /media/sndp/DATA/www/
drwxrwxr-x 2 sndp     sndp     4096 Mar 14 15:57 .
drwxrwxr-x 8 sndp     sndp     4096 Mar 14 15:57 ..
-rwxr-xr-x 1 www-data www-data   17 Mar 13 13:47 index.php

标签: ubuntunginxserver

解决方案


推荐阅读