nginx 403 can't access picture resources

Keywords: Nginx

Open a new server and configure nginx

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  www.auceo.cn;
        root         /data/www;

        include /etc/nginx/default.d/*.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { 
   
            root /data/www;   
            expires      7d; 
        }  
The problem is that index.html can access the image, the configuration image can't access it, and after a long time of searching for an error, why can't I access the image, I find out that it was the user's access right,

user nginx;
The default is above, so it cannot be accessed. Then change to

user root root;
Then you can show the pictures during the visit. There are no 403 questions.

But to avoid using Root directly, you can create www users and user groups

Then directly change the / data directory and subdirectory to www user

[root@VM_58_190_centos www]# chown -R www:www /data
drwxr-xr-x   5 www  www   4096 Jan  9 13:50 data
Then nginx conf is configured as follows

user www www;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  www.auceo.cn;
        root         /data/www;

        include /etc/nginx/default.d/*.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { 
   
            root /data/www;   
            expires      7d; 
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

Posted by drax007 on Sat, 02 May 2020 11:58:00 -0700