仅在项目的某个目录上的 HTTPServer - NGINX

HTTPServer only on a certain directory of project - NGINX

目标

我的目标是在访问我的应用程序的某个 URL 时看到这样的内容:


尝试

location /backup {
    autoindex on;
    index index.html; 
    root /home/app/public/backup;
}

结果


配置

我的整个vi /etc/nginx/sites-available/default

server {

    listen 80 default_server;
    server_name default;
    root /home/app/public;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    index index.html index.htm index.php;

    charset utf-8;

    location /backup {
        autoindex on;
        index index.html;
        root /home/app/public/backup;
    }


    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    access_log off;
    error_log  /var/log/nginx/default-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

根据您的配置,nginx 正在尝试定位 /home/app/public/backup/backup 路径,因此 404

这 3 种解决方案适合您

# remove root directive (recommended in your case)
location /backup {
    autoindex on;
    index index.html;
    #root /home/app/public/backup;
}

# set root to public (redundant since already done)
location /backup {
    autoindex on;
    index index.html;
    root /home/app/public;
}

# replace root with alias
location /backup {
    autoindex on;
    index index.html;
    alias /home/app/public/backup/;
}

有关详细信息,请查看 root and alias work, you can also read how nginx handles user requests