How to config nginx to run angular4 application

前端 未结 3 1451
深忆病人
深忆病人 2020-12-13 15:14

I am new to angular and I would like to put the application on Nginx server on a docker container.

1. Running the angular application through @angular

3条回答
  •  余生分开走
    2020-12-13 15:50

    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: 8080 Note - HOST and PORT might be different in your case.

    Make sure you have 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.

提交回复
热议问题