使用 nginx 服务 html / css / js 文件

Serving html / css / js files with nginx

这是我的静态网站目录树:

- website
-- html
--- index.html
-- css
--- styles.css
-- js

这是我的 nginx 配置:

server {
    listen 80;

    root /srv/www/domain.com/website/html;
    index index.php index.html index.htm;

    server_name domain.com;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

但是它无法加载 css / js 文件,它说 404 未找到。怎么了?

   location ~* \.(js|jpg|png|css)$ {
        root /srv/www/domain.com/website/;
        expires 30d;
    }

解决了我的问题。