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

时间:2017-04-28 17:17:59

标签: laravel http nginx ftp webserver

目标

当我访问我的应用的某个网址时,我的目标是看到类似的内容:

enter image description here

尝试

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

结果

enter image description here

配置

我的整个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;
    }
}

1 个答案:

答案 0 :(得分:3)

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

这三种解决方案适合您

# 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;
}

有关详情,请查看rootalias的工作原理,您还可以阅读nginx handles user requests

的方式