我試圖設定 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 (這就是全部):
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 檔案,但這就是它的構建方式并且在本地運行良好。
uj5u.com熱心網友回復:
您必須先運行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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/525895.html