Example method of deploying react project on nginx

Example method of deploying react project on nginx

Test project: react-demo

  1. Clone your react-demo project to the server (Github is used to manage our project by default)
  2. If necessary, please install the project environment, such as: node.js, yarn, etc.
  3. Enter the project directory and execute npm run build to start building the project
  4. After the build is successful, a dist folder will be generated (depending on your project configuration). The static files in this folder are the access files of our project.
  5. To configure Nginx, on a Linux server, go to: /etc/nginx/sites-enabled, then as an administrator, create a new configuration file for your react project, such as: react-demo.conf, and then edit the file:
server {
  listen 8080;
  # server_name your.domain.com;
  root /home/root/react-demo/dist;
  index index.html index.htm;
  location / {
    try_files $uri $uri/ /index.html;
  }
  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }
  error_page 500 502 503 504 /500.html;
  client_max_body_size 20M;
  keepalive_timeout 10;
}

Execute sudo service nginx restart to restart the Nginx service.

Access the project, http://IP:8080/

Note:

1. If you configure the domain name, port 80 is required. After success, you can access the project by simply accessing the domain name

2. If you use the browserHistory mode of React-Router, please add the following configuration to the Nginx configuration:

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

Principle, because our project has only one root entry, when entering a URL like /home, the page cannot be found. In this case, nginx will try to load index.html. After loading index.html, react-router will work and match the /home route we entered, thereby displaying the correct home page. If the project in browserHistory mode is not configured with the above content, a 404 error will occur.

Please refer to the react-router documentation:

https://react-guide.github.io/react-router-cn/docs/guides/basics/Histories.html

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Threejs realizes the earth animation function on the homepage of Didi official website
  • Using 3D engine threeJS to realize star particle movement effect
  • Detailed explanation of how to modify the packaging address (compilation output file address) of the React project
  • Detailed explanation of installing and using Less in React projects (usage summary)
  • How to use iconfont in react project
  • How to deploy React project using Jenkins
  • Detailed process of creating a VR panoramic project using React and Threejs

<<:  jQuery realizes the effect of theater seat selection and reservation

>>:  The latest version of MySQL5.7.19 decompression version installation guide

Recommend

Basic syntax of MySQL index

An index is a sorted data structure! The fields t...

JavaScript code to achieve a simple calendar effect

This article shares the specific code for JavaScr...

Detailed steps to use Redis in Docker

1. Introduction This article will show you how to...

JavaScript implements fireworks effects with sound effects

It took me half an hour to write the code, and th...

Detailed explanation of MySQL transactions and MySQL logs

Transactional Characteristics 1. Atomicity: After...

base target="" controls the link's target open frame

<base target=_blank> changes the target fram...

A detailed summary of HTML tag nesting rules suitable for beginners

I have been relearning HTML recently, which can be...

Network configuration of Host Only+NAT mode under VirtualBox

The network configuration of Host Only+NAT mode u...

How to enable Swoole Loader extension on Linux system virtual host

Special note: Only the Swoole extension is instal...

SQL group by to remove duplicates and sort by other fields

need: Merge identical items of one field and sort...

Neon light effects implemented with pure CSS3

This is the effect to be achieved: You can see th...

Stealing data using CSS in Firefox

0x00 Introduction A few months ago, I found a vul...