简体   繁体   English

Nginx 到 node.js 的反向代理不起作用

[英]Nginx reverse proxy to node.js not working

I have an angular and node app with Postgres as the db.我有一个以 Postgres 作为数据库的角度和节点应用程序。 I am deploying them to docker containers on ec2 instance.我正在将它们部署到 ec2 实例上的 docker 容器。 The Nginx reverse proxy on ec2 is not routing requests to node. ec2 上的 Nginx 反向代理不会将请求路由到节点。 The same setup works on my local machine.相同的设置适用于我的本地机器。 The error message I receive is: " XMLHttpRequest cannot load http://localhost:3000/api/user/login due to access control checks ".我收到的错误消息是:“由于访问控制检查,XMLHttpRequest 无法加载 http://localhost:3000/api/user/login ”。 Here is my docker-compose file:这是我的 docker-compose 文件:

version: '3.3'

networks:
  network1:

services:
  api-service:
    image: backend
    environment:
      PORT: 3000 
    volumes:
      - ./volumes/logs:/app/logs
    ports:
      - 3000:3000
    restart: always
    networks:
      - network1

  nginx:
    image: frontend
    environment:
      USE_SSL: "false"
    volumes:
      - ./volumes/logs/nginx:/var/log/nginx/
      - ./volumes/ssl/nginx:/etc/ssl/
    ports:
      - 80:80
      - 443:443
    restart: always
    networks:
      - network1

and my Nginx.conf is as follows:我的 Nginx.conf 如下:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    server {
        listen       80;
    

        location / {
            root /myapp/app;
            try_files $uri $uri/ /index.html;


            proxy_set_header Upgrade    $http_upgrade;
            proxy_set_header Connection $http_connection;
            proxy_set_header Host       $host;
        }

        location /api/ {
            proxy_pass http://api-service:3000;
        }
    }
}


and my api url for backend is : (the commented lines are all what I have tried)我的后端 api url 是:(注释行都是我尝试过的)

export const environment = {
  production: false,
   //apiUrl: 'http://api-service'
  // apiUrl: '/api-service/api'
  // apiUrl : 'http://' + window.location.hostname + ':3000'
  //apiUrl: 'http://localhost:3000/api'
  apiUrl: 'http://localhost:3000'
};

and in my angular service I am using:
const BACKEND_URL = environment.apiUrl + "/api/user/";

and in my backend app.js:

app.use("/api/user/", userRoutes);

What am I doing wrong?我究竟做错了什么?

Posting what worked for me in case it helps someone.发布对我有用的内容,以防它对某人有所帮助。 Here is what worked when I moved my code to ec2 instance:当我将代码移动到 ec2 实例时,这是有效的:

export const environment = { production: false, //apiUrl: 'http://localhost:3000/api' apiUrl: '/api' }; export const environment = { production: false, //apiUrl: 'http://localhost:3000/api' apiUrl: '/api' };

So, in localhost apiUrl: 'http://localhost:3000/api works but it does not work on server.因此,在 localhost apiUrl 中:'http://localhost:3000/api 有效,但在服务器上无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM