请求nginx静态资源报403
Posted PacosonSWJTU
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请求nginx静态资源报403相关的知识,希望对你有一定的参考价值。
【README】
请求nginx静态资源报403;
【1】原因
静态资源防止在某个家目录下,如 /root 目录下
【2】 解决方法1
nginx.conf 文件没有指定用户
# user nobody
修改为
user root; # 设置为root用户 ;
【例子】
user root; #使用root用户访问
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 {
# load balance config
upstream myserver {
# ip_hash;
# fair;
server 192.168.163.201:8080 weight=10;
server 192.168.163.204:8080 weight=50;
}
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 192.168.163.204;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location / {
proxy_pass http://myserver;
root html;
index index.html index.htm;
}
location /test01 {
proxy_pass http://myserver/test01/;
}
location /test02 {
proxy_pass http://myserver/test02/;
}
location /www/{
root /root/software/nginx/data; #root家目录下的静态资源
index index.html index.htm;
}
location /image/ {
root /root/software/nginx/data;
autoindex on;
}
}
}
访问效果
【3】解决方法2
把nginx静态资源放置在其他地方, 如 /home/nginx下,并将其权限设置为777 ;
【例子】
静态资源目录结构如下:
[root@localhost data]# pwd
/home/nginx/data
[root@localhost data]# tree
.
├── image2
│ └── home.png
└── www2
└── home.html
2 directories, 2 files
[root@localhost data]# ll
total 0
drwxr-xr-x. 2 root root 22 Aug 7 20:07 image2
drwxr-xr-x. 2 root root 23 Aug 7 20:07 www2
[root@localhost data]# cd ..
[root@localhost nginx]# ll
total 0
drwxrwxrwx. 4 root root 32 Aug 7 20:22 data #777
nginx配置
#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 {
# load balance config
upstream myserver {
# ip_hash;
# fair;
server 192.168.163.201:8080 weight=10;
server 192.168.163.204:8080 weight=50;
}
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 192.168.163.204;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location / {
proxy_pass http://myserver;
root html;
index index.html index.htm;
}
location /test01 {
proxy_pass http://myserver/test01/;
}
location /test02 {
proxy_pass http://myserver/test02/;
}
location /www/{
root /root/software/nginx/data;
index index.html index.htm;
}
location /image/ {
root /root/software/nginx/data;
autoindex on;
}
location /www2/{
root /home/nginx/data/; # static resource located in home dir
index index.html index.htm;
}
location /image2/ { # static resource located in home dir
root /home/nginx/data/;
autoindex on;
}
}
}
访问效果
以上是关于请求nginx静态资源报403的主要内容,如果未能解决你的问题,请参考以下文章
nginx静态资源文件无法访问,403 forbidden错误