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

Linux file/directory permissions and ownership management

1. Overview of file permissions and ownership 1. ...

Summary of Vue's cross-domain problem handling and solutions

When you send a network request, the following sa...

Uniapp WeChat applet: Solution to key failure

uniapp code <template> <view> <ima...

Three ways to implement virtual hosts under Linux7

1. Same IP address, different port numbers Virtua...

Detailed tutorial on building nextcloud private cloud storage network disk

Nextcloud is an open source and free private clou...

A brief analysis of the basic concepts of HTML web pages

What is a web page? The page displayed after the ...

A method of making carousel images with CSS3

Slideshows are often seen on web pages. They have...

Vue implements online preview of PDF files (using pdf.js/iframe/embed)

Preface I am currently working on a high-quality ...

Dissecting the advantages of class over id when annotating HTML elements

There are very complex HTML structures in web pag...

Completely uninstall MySQL database in Windows system to reinstall MySQL

1. In the control panel, uninstall all components...

How CSS affects the white screen time during initial loading

Rendering pipeline with external css files In the...