【问题标题】:Deploy Gatsby js static site on Nginx VPS在 Nginx VPS 上部署 Gatsby js 静态站点
【发布时间】:2019-10-13 13:00:46
【问题描述】:

基本上,我使用 gatsby build 命令构建了我的 gatsby 站点,我想使用 Nginxpublic 目录提供所有静态文件。

我的网站位于/var/www/mywebsite,还有一些测试html 基本页面位于/var/www/test。 我可以提供该测试页面并且它工作正常,但是当我将目录更改为该 gatsby 站点时它不起作用并且我得到Forbiden 403 error。 我认为这可能是我的配置,但我是否必须对 gatsby 做一些特别的事情才能正确地提供所有这些静态文件?

nginx.conf

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;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

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

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

    server {
        server_name mywebsite.com www.mywebsite.com; # managed by Certbot
        # root /var/www/test;   # <-- This gets served
        root /var/www/mywebsite/public;  # <-- This doesn't
        index index.html index.html;

        location / {
            try_files $uri $uri/ /index.html;
        }

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

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

        # some ssl certificates...
    }

    server {
        if ($host = www.mywebsite.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
        if ($host = mywebsite.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot

        listen       80;
        listen       [::]:80;
        server_name mywebsite.com www.mywebsite.com;
        return 404; # managed by Certbot
    }
}

我读到我不需要 sites-availablesites-enabled 目录,它可以在主 nginx.conf 文件中配置,也可以从 nginx.conf 本身的 conf.d 目录中包含。

【问题讨论】:

    标签: reactjs nginx digital-ocean gatsby static-site


    【解决方案1】:

    禁止错误可能与权限问题有关。
    您可以通过在 /var/www 中运行 ls -al 来检查权限。

    运行sudo chown www-data -R /var/www,看看问题是否仍然存在。 如果这不能解决问题,请运行:

    sudo tail -f /var/log/nginx/error.log
    

    并检查日志以了解问题所在。或在您的问题中包含日志。

    【讨论】:

    • 该命令单独不起作用,它仍然说:权限被拒绝,无法访问 index.html 我使用此命令“chcon -Rt httpd_sys_content_t /var/www”并且它起作用了。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2019-04-04
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2018-09-13
    相关资源
    最近更新 更多