导航到图像文件NGINX时出现404错误

时间:2019-08-18 17:02:35

标签: image nginx server

我无法导航到图像文件。当我尝试访问http://example.com/public/images/header.png上的页面时,收到404错误。

我确保该图像存在于/var/www/example.com/public/images/文件夹中,但是尝试访问上述链接时我仍然收到404错误。这是我的NGINX服务器块配置:

server {
    listen 80;
    listen [::]:80;
    root /var/www/example.com/public;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name example.com www.example.com;

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }

    location /images/ {
        alias /var/www/example.com/public/images/;
        add_header  Cache-control "public";
        access_log  off;
        expires 90d;
        autoindex on;
        autoindex_exact_size off;
    }

    include /var/www/example.com/.nginx.conf;
}

您可以看到我的网站不在/ public文件夹中。我有一个可以访问example.com的Flarum论坛。我希望能够在论坛上提供图片作为背景,但是我什至无法让服务器在example.com/public/images/header.png上简单地显示图片。有人可以向我解释我可能做错了什么吗?

2 个答案:

答案 0 :(得分:0)

因此,我通过将所有http请求重定向到https并通过将config.php(Flarum核心的一部分)文件调整为在域URL中包含https://而不是http://来解决了这一问题。这是新编辑的服务器块和config.php文件:

server {
    listen 80;
    # server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot

    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    server_name example.com www.example.com;

    add_header Strict-Transport-Security "max-age=31536000" always; 

    root /var/www/example.com/public;

    index index.php index.html index.htm index.nginx-debian.html;

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }

    location /images/ {
        alias /var/www/example.com/public/images/;
        add_header  Cache-control "public";
        access_log  off;
        expires 90d;
        autoindex on;
        autoindex_exact_size off;
    }

    include /var/www/example.com/.nginx.conf;
}

config.php:

<?php return array (
  'debug' => true,
  'database' => 
  array (
    'driver' => 'mysql',
    'host' => 'localhost',
    'port' => 3306,
    'database' => '',
    'username' => '',
    'password' => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => 'InnoDB',
    'prefix_indexes' => true,
  ),
  'url' => 'https://example.com',
  'paths' => 
  array (
    'api' => 'api',
    'admin' => 'admin',
  ),
);

答案 1 :(得分:0)

您的root设置为/var/www/example.com/public,那么为什么要在http://example.com/public/images/header.png上访问图像呢?

可访问的链接为http://example.com/images/header.png

您最近发布的配置在图像位置中包含无效的alias。它应该很简单,没有alias

location /images/ {
    add_header  Cache-control "public";
    access_log  off;
    expires 90d;
    autoindex on;
    autoindex_exact_size off;
}