我在 Amazon Web Server 中運行了一個連接到 Express APIREST 的 ReactJS 前端。使用 PM2 啟動行程和 NGINX。
當我嘗試在 chrome 中登錄應用程式時出現問題,前端沒問題,但拒絕連接到 APIREST。 錯誤影像
這些是我擁有的檔案:
APIREST .env
DB_CCT=my_db
DB_CCT_USER=user
DB_CCT_PASS=password
DB_CCT_HOST=127.0.0.1
NODE_ENV=development
PORT=8017
JWT_SECRET=jotauvedoblete
前端 ReactJS .env
我還嘗試添加公共 IP 而不是 localhost 或 127.0.0.1 但也不起作用,它顯示連接超時。
REACT_APP_DB=http://localhost:8017/api/v1/
PM2生態系統.config.js
module.exports = {
apps: [{
name: 'CCT_REACT',
cwd: 'cct-web-client/',
script: 'npm',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'run start:production',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 5001
},
env_production: {
NODE_ENV: 'production',
PORT: 5001
}
},
{
name: 'CCT_API_REST',
cwd: 'cct_api_rest/',
script: 'npm',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'run start',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 8017
},
env_production: {
NODE_ENV: 'production',
PORT: 8017
}
}],
deploy: {
production: {
}
}
}
NGINX服務
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/ubuntu/CCT/cct-web-client/build;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri/ /index.html?q=$uri&$args;
}
}
uj5u.com熱心網友回復:
如果您的連接被拒絕,這意味著請求被阻止。
我看到您為 API 應用程式指定了 PORT 8017,請檢查兩件事 -
- 檢查您的 AWS 安全組 EC2 入站規則 PORT 8017對所有人開放。
- 也嘗試重新啟動 Nginx 一次 - `sudo systemctl restart Nginx。
- 對于您的 API,只需在沒有 pm2 的情況下啟動您的 API,并在您的路由和中間件中添加一個 console.log,然后從 POSTMAN 發出一個 POST 請求,以檢查您的應用程式是否以正確的方式運行。
uj5u.com熱心網友回復:
允許連接到 aws 上的埠 80 并洗掉所有其他埠并使用以下 nginx 配置。還要將前端中的 api 后端 url 設定為您的域或埠 80 上的 ip。
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/ubuntu/CCT/cct-web-client/build;
index index.html index.htm index.nginx-debian.html;
server_name _;
# Frontend
location / {
try_files $uri /index.html;
}
# node api reverse proxy
location /api/ {
proxy_pass http://localhost:8017/;
}
}
如果您可以在沒有前端的情況下使用您的 api,請確保首先檢查任何 api 客戶端。它不應該是超時。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/323898.html
標籤:javascript 反应 表达 nginx 下午2
上一篇:使用Docker Nginx Yii2在Redis上的會話問題
下一篇:Nuxt不創建dist目錄
