Im trying to setup nginx but it seems nginx does not have permissions to do what it has to.

I’m running an EC2 instance the delivers a React frontend with a Node.js backend.

/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"

The only thing I changed in nginx.conf (because it is the correct owner of the directory):

user ubuntu;

I check owner like this:

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

and get:

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;
           }
}

Any idea of what I’m doing wrong?

SELinux is disabled.

EDIT:
As you can see, there is no index.html file in the React app folder, but this is how it is built and works well locally.

you will have to run npm run build first, then point the nginx in to the newly created build directory

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;
           }
}

4

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *