【问题标题】:AWS EC2 instance - Nginx returns 403 forbiddenAWS EC2 实例 - Nginx 返回 403 禁止
【发布时间】:2022-11-01 11:04:17
【问题描述】:

我试图设置 nginx,但似乎 nginx 没有权限去做它必须做的事情。

我正在运行一个 EC2 实例,它提供一个带有 Node.js 后端的 React 前端。

/var/log/nginx/error.log:

2022/11/01 02:01:24 [error] 2426#2426: *4 "/home/ubuntu/apps/myapp-frontend/index.html" is forbidden (13: Permission denied), client: the.client.ip, server: the.server.ip, request: "GET / HTTP/1.1", host: "the.server.ip"

我在 nginx.conf 中更改的唯一内容(因为它是目录的正确所有者):

user ubuntu;

我这样检查所有者:

sudo ls -l /home/ubuntu/apps/myapp-frontend/

并得到:

total 1960
-rw-rw-r--    1 ubuntu ubuntu      21 Oct 31 20:28 README.md
drwxrwxr-x 1096 ubuntu ubuntu   36864 Oct 31 20:37 node_modules
-rw-rw-r--    1 ubuntu ubuntu 1431930 Oct 31 20:29 package-lock.json
-rw-rw-r--    1 ubuntu ubuntu    1203 Oct 31 20:28 package.json
drwxrwxr-x    2 ubuntu ubuntu    4096 Oct 31 20:28 public
-rw-rw-r--    1 ubuntu ubuntu   30795 Oct 31 20:28 react-jwt-authentication-flow.png
-rw-rw-r--    1 ubuntu ubuntu   17260 Oct 31 20:28 react-jwt-authentication-project-overview.png
drwxrwxr-x    6 ubuntu ubuntu    4096 Oct 31 20:28 src
-rw-rw-r--    1 ubuntu ubuntu  462013 Oct 31 20:29 yarn.lock

/etc/nginx/sites-available/myserver (this is ALL of it):

server {
        listen 80;
        listen [::]:80;
        root /home/ubuntu/apps/myapp-frontend;
        index index.html index.htm index.nginx-debian.html;
        server_name website.ip.address;
        location / {
                try_files $uri $uri/ =404;
                   }
        location /api {
            proxy_pass http://localhost:8080;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
           }
}

知道我做错了什么吗?

SELinux 被禁用。

编辑: 如您所见,React 应用程序文件夹中没有 index.html 文件,但这就是它的构建方式,并且在本地运行良好。

【问题讨论】:

    标签: nginx amazon-ec2 nginx-config


    【解决方案1】:

    您必须先运行npm run build,然后将 nginx 指向新创建的构建目录

    server {
            listen 80;
            listen [::]:80;
            root /home/ubuntu/apps/myapp-frontend/*build*;
            index index.html index.htm index.nginx-debian.html;
            server_name website.ip.address;
            location / {
                    try_files $uri $uri/ =404;
                       }
            location /api {
                proxy_pass http://localhost:8080;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
               }
    }

    【讨论】:

    • 我试过这个,但是当我运行 npm run build 时整个实例崩溃了。
    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 2019-04-17
    • 2020-07-19
    相关资源
    最近更新 更多