开门见山,该错误肯定是由于权限不足导致的,先把基调定下来,再从中一步步查。
2、需求背景需要通过访问http://192.168.1.54:8080/statics/ 访问到服务器上 /home/zhangsan/webApp/statics 目录及其下资源
3、访问报错4、nginx配置
#user nobody;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#error_log logs/error.log crit;
pid logs/nginx.pid;
worker_rlimit_nofile 3000;
events
{
use epoll;
worker_connections 1000;
}
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;
server_names_hash_bucket_size 128; ##服务器名字的hash表大小##
client_header_buffer_size 32k; ##上传文件大小限制##
large_client_header_buffers 4 64k; ##设定请求缓##
client_max_body_size 8m; ##设定请求缓##
sendfile on; ##开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,##
tcp_nopush on; ##防止网络阻塞##
tcp_nodelay on; ##防止网络阻塞##
#keepalive_timeout 0;
keepalive_timeout 120;
##gzip模块设置##
gzip on; ##开启gzip压缩输出##
gzip_min_length 1k; ##最小压缩文件大小##
gzip_buffers 4 16k; ##压缩缓冲区##
gzip_http_version 1.0; ##压缩版本(默认1.1,前端如果是squid2.5请使用1.0)##
gzip_comp_level 2; ##压缩等级##
gzip_types text/plain application/x-javascript text/css application/xml;
##压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn
gzip_vary on;
#limit_zone crawler 10m; ##开启限制IP连接数的时候需要使用##
server {
listen 8080;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.htm;
}
location /statics/ {
root /home/zhangsan/webApp;
autoindex on;#自动列出文件
autoindex_exact_size on;#精确列表显示文件格式,包括大小 kb mb gb
autoindex_localtime on;#显示文件时间
#limit_rate 1k;#指定每秒该连接能下载的bytes,主要用来限制个别请求的带宽
auth_basic string;
}
#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 html;
}
}
}
5、解决
nginx运行账号无访问权限
而 /home/zhangsan/webApp/statics 目录是在 用户 zhangsan 家目录下的,nobody账号无权限访问 。
解决:
方法一:将配置文件中第一行 #user nobody; 改成 user root; 在重载nginx
方法二:将/home/zhangsan/webApp/statics 目录每一层都的权限都设置为其他用户可见
chmod o+rx /home/zhangsan/
chmod o+rx webApp
chmod o+rx statics
6、总结要想能访问到服务器上的资源,必须满足如下条件:
运行nginx进程的账号对资源和其所在的目录有读取和执行权限(包括每一级父目录)


