2 xyq167477 xyq167477 于 2017.01.06 16:36 提问

求帮助,Linux安装Nginx后改了根目录报403

没改之前还有欢迎。。。。。啥的改了之后图片说明
我修改的是下面三个地方,原来的都是html;
图片说明
图片说明
除了报403,里面的图片可以看图片说明
关于权限,我都设置了777
图片说明
图片说明
图片说明

9个回答

u010528826
u010528826   2017.01.06 19:47
已采纳

我的意思是先把:
location / {
root /home/ftpuser/www;
index index.html index.htm;
}
还原成:
location / {
root html;
index index.html index.htm;
}
再新增新增一个location:
location ~ .*.(gif|jpg|jpeg|png)$ {
root /home/ftpuser/www;
index index.html index.htm;
}

u010528826
u010528826 回复xyq167477: 再配置具体的location就行了。说白了每个location都表示一个规则,你为你不同的访问请求配置不同的规则就行了,但是要注意先后顺序。同时也要注意访问不同的资源,需要配置的属性也不同。比如访问html需要使用的属性,访问图片则不需要使用。具体的你去搜索一下Nginx location,理解了就简单了
11 个月之前 回复
xyq167477
xyq167477 大哥谢谢啦
11 个月之前 回复
xyq167477
xyq167477 怎么设置呢?
11 个月之前 回复
xyq167477
xyq167477 这个只能访问图片,能不能设置成所有的的文件呢?像啥txt,doc之类的
11 个月之前 回复
xyq167477
xyq167477   2017.01.06 17:29

图片说明

oyljerry
oyljerry   Ds   Rxr 2017.01.06 17:33

注意路径的拼接,你可以看看nginx的error.log中会有最后拼接访问的路径,这样就知道你修改路径后,为什么拼接的路径不正确了

xyq167477
xyq167477 额,怎么看这个拼接规则呀。。。。。。
11 个月之前 回复
xyq167477
xyq167477   2017.01.06 18:10
 #user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/ftpuser/www;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/ftpuser/www;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /home/ftpuser/www;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           /home/ftpuser/www;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
``

u010528826
u010528826   2017.01.06 18:19

我没记错的话,root后面的目录是从Ng的安装根目录开始算起。照你这个配置,应该会找到ng的安装根目录/home/ftpuser/www。不知道说的对不对

u010528826
u010528826 抱歉刚才没看懂你的问题。你是说你改了配置之后,图片可以正确访问,但是其他请求报错是吧?我看你把所有请求
11 个月之前 回复
xyq167477
xyq167477 /home/ftpuser/www是我用来存图片的
11 个月之前 回复
xyq167477
xyq167477 ng的安装目录是/usr/local/nginx/sbin/
11 个月之前 回复
u010528826
u010528826   2017.01.06 18:41

抱歉刚才没看懂你的问题。你是说你改了配置之后,图片可以正确访问,但是其他请求报错是吧?我看你把所有请求全部指向了/home/ftpuser/www
而这个文件夹里面只放了图片,所以你能正确访问图片,其他请求会报错。
这里应该改一下:
location / {
root /home/ftpuser/www;
index index.html index.htm;
}
先还原。任何新增一个location:
location ~ .*.(gif|jpg|jpeg|png)$ {
root /home/ftpuser/www;
index index.html index.htm;
}

xyq167477
xyq167477 嗯,文本也能访问
11 个月之前 回复
xyq167477
xyq167477   2017.01.06 19:27
         #access_log  logs/host.access.log  main;

        location / {
            root   /home/ftpuser/www;
            index  index.html index.htm;
        }
    location ~ .*.(gif|jpg|jpeg|png)$ {
        root   /home/ftpuser/www;
        index  index.html index.htm;
    }
        #error_page  404              /404.html;

改成这样么?

xyq167477
xyq167477   2017.01.06 19:37

我的想法就是输入http://192.168.44.128/ 后显示那个Nginx的欢迎页面

xyq167477
xyq167477   2017.01.06 19:39

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!