How to config nginx to run angular4 application How to config nginx to run angular4 application nginx nginx

How to config nginx to run angular4 application


If anyone still struggling with production setup of angular 2/4/5 app + Nginx(i.e. Without Docker), then here is the complete solution:

Suppose you want to deploy your angular app at HOST: http://example.com and PORT: 8080Note - HOST and PORT might be different in your case.

Make sure you have <base href="/"> in you index.html head tag.

  1. Firstly, go to your angular repo (i.e. /home/user/helloWorld) path at your machine.

  2. Then build /dist for your production server using the following command:

    ng build --prod --base-href http://example.com:8080

  3. Now copy /dist (i.e. /home/user/helloWorld/dist) folder from your machine's angular repo to the remote machine where you want to host your production server.

  4. Now login to your remote server and add following nginx server configuration.

    server {    listen 8080;    server_name http://example.com;    root /path/to/your/dist/location;    # eg. root /home/admin/helloWorld/dist    index index.html index.htm;    location / {        try_files $uri $uri/ /index.html;        # This will allow you to refresh page in your angular app. Which will not give error 404.    }}
  5. Now restart nginx.

  6. That's it !! Now you can access your angular app at http://example.com:8080

Hope it will be helpful.


One thing that's wrong is that you are trying to publish your source files, instead of doing a production build using the cli and publish the output of these files. ng start is used for local development. When you are happy with your outcome, you can use ng build --prod to build your application, and whatever resides in your /dist folder should be placed in the docker.

If you want to have everything in your docker, you should ng build --prod after creating your new project, and then point the root of your nginx to /var/www/frontend/dist;. This will highly increase the boot time of your docker though. Obviously depending on the size of your project


If anyone still have problems serving angular with docker and ssl.

Dockerfile

FROM nginx:stable## From ‘builder’ stage copy over the artifacts in dist folder to default nginx public folderCOPY --from=builder /app/dist/my-app /usr/share/nginx/html/## Skip this if you are using kubernetes config map COPY nginx.conf /etc/nginx/nginx.conf## ServeCMD ["nginx", "-g", "daemon off;"]

Provide nginx.conf via kubernetes config map definition as follows:

volumeMounts:- mountPath: /etc/nginx/nginx.conf  name: config  subPath: nginx.confvolumes:- configMap:    defaultMode: 420    name: config-map-name

nginx.conf

worker_processes auto;pid /tmp/nginx.pid;events {  worker_connections 1024;  use epoll;  multi_accept on;}http {  # this is necessary for us to be able to disable request buffering in all cases  include       /etc/nginx/mime.types;  default_type  application/octet-stream;  keepalive_timeout  65;  log_format timed_combined 'remote_addr - '    '"$request" $status $body_bytes_sent '    '"$http_referer" "$http_user_agent" '    '$request_time $upstream_response_time $pipe';  access_log /dev/stdout timed_combined;  server {    listen 0.0.0.0:443 ssl;    listen [::]:443 ssl default_server;    server_tokens off;    root /usr/share/nginx/html;    # SSL    ssl_certificate /etc/nginx/cert/tls.crt;    ssl_certificate_key /etc/nginx/cert/tls.key;    # Recommendations from https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html    ssl_protocols TLSv1.1 TLSv1.2;    ssl_ciphers '!aNULL:kECDH+AESGCM:ECDH+AESGCM:RSA+AESGCM:kECDH+AES:ECDH+AES:RSA+AES:';    ssl_prefer_server_ciphers on;    ssl_session_cache shared:SSL:10m;    # disable any limits to avoid HTTP 413 for large image uploads    client_max_body_size 0;    # required to avoid HTTP 411:    chunked_transfer_encoding on;    location / {        try_files $uri$args $uri$args/ /index.html;    }  }}