How to configure Beanstalk Nodejs Nginx server to serve a nodejs app and also other static apps built using Angular?

0

Hi, I am using AWS Beanstalk to run a Nodejs 18 nginx server using a Procfile and it is working.
I tried to configure it using Nginx config files to show other static files and also single-page apps built using Angular but the server can't return them.

These are my nginx conf files.

in nginx.conf

#Elastic Beanstalk Nginx Configuration File

user                    nginx;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    31486;

events {
    worker_connections  1024;
}

http {
    server_tokens off;

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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    include       conf.d/*.conf;

    map $http_upgrade $connection_upgrade {
        default     "upgrade";
    }

    server {
        listen        80 default_server;
        access_log    /var/log/nginx/access.log main;

        client_header_timeout 60;
        client_body_timeout   60;
        keepalive_timeout     60;
        gzip                  off;
        gzip_comp_level       4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;
    }
}

in conf.d/elasticbeanstalk/application.conf

location / {
    try_files $uri $uri/ /index.html;
}

location /singlePageApp/ {
  if (!-e $request_filename){
    rewrite ^/singlePageApp/(.*)$ /singlePageApp/index.html break;
  }
}

location /nodeApp/ {
    # Proxy requests to Node.js app running on port 8080
    proxy_pass          http://127.0.0.1:8080;
    proxy_http_version  1.1;

    proxy_set_header    Connection          $connection_upgrade;
    proxy_set_header    Upgrade             $http_upgrade;
    proxy_set_header    Host                $host;
    proxy_set_header    X-Real-IP           $remote_addr;
    proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
}
질문됨 일 년 전876회 조회
1개 답변
0
수락된 답변

Hi from what you were writing I understand, that you want to bypass the nodejs server to serve your static files for the single page app.

To do so you have to point nginx to the right folder in the file system where Elastic Beanstalk deployed your app to. You do so with the root element, e.g. root /var/www/main;. For more detailed information see https://www.nginx.com/blog/creating-nginx-rewrite-rules/ and https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms.

If runtime/performance is not an issue it might be easier to configure your nodejs server to serve the static files for the single page app, too and proxy_pass the location /singlePageApp/ accordingly, too.

AWS
Norman
답변함 일 년 전
profile picture
전문가
검토됨 16일 전
  • Thank you, I figured it out after reading the digital ocean document. I wasn't trying to bypass the nodejs server to serve my static files for the single page app but the nodejs server was built using Angular Universal which is a standalone nodejs app so it doesn't have access to the other files by default.

    Btw, the root for Elastic Beanstalk is root /var/app/current;

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠