How to configure the same domain name for the front and back ends of nginx

How to configure the same domain name for the front and back ends of nginx

This article mainly introduces the method of configuring the same domain name for the front-end and back-end of nginx, and shares it with you. The details are as follows:

upstream dfct {
#ip_hash;
 server 121.41.19.236:8192;
}
 
server {
 server_name ct.aeert.com;
 
 location / {
  root /opt/web;
  try_files $uri $uri/ /index.html;
  error_page 405 =200 http://$host$request_uri;
 }
 
 location ^~/web/ {
  proxy_set_header Host $proxy_host;
# proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://121.41.19.236:8192/;
 }
 
 
 listen 443 ssl; # managed by Certbot
 ssl_certificate /etc/letsencrypt/live/ct.aeert.com/fullchain.pem; # managed by Certbot
 ssl_certificate_key /etc/letsencrypt/live/ct.aeert.com/privkey.pem; # managed by Certbot
 include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
 
}
 
 
server {
 if ($host = ct.aeert.com) {
  return 301 https://$host$request_uri;
 } # managed by Certbot
 
 
 listen 80;
 server_name ct.aeert.com;
 return 404; # managed by Certbot
 
 
}

Supplement: Three ways to deploy projects with separated front-end and back-end using nginx

For projects with separated front-end and back-end, the front-end and back-end can use different domain names or the same domain name.

The following is the case where the frontend and backend use the same domain name:

1. The front end uses www.xxx.com and the back end uses api.xxx.com

server {
server_name www.xxx.com;

location / {
 root /tmp/dist;
 index index.html;
 try_files $uri $uri/ /index.html;
  }
 }

server {
server_name api.xxx.com;
location / {
uwsgi_pass 127.0.0.1:8000;
include /etc/nginx/uwsgi_params;
 }
}

2. The front end uses www.xxx.com and the back end uses www.xxx.com/api/

1. If uwsgi uses http, it can be configured like this

server {
server_name www.xxx.com;

location / {
 root /tmp/dist;
 index index.html;
 try_files $uri $uri/ /index.html;
 }

location ^~ /api/ {
 proxy_pass http://127.0.0.1:8000/;
 }
}

2. If uwsgi uses the socket method, it needs to be configured like this

server {
server_name www.xxx.com;
location / {
 root /tmp/dist;
 index index.html;
 try_files $uri $uri/ /index.html;
}

location ^~ /api/ {
 proxy_pass http://127.0.0.1:8080/;
 }
}
server {
listen 8080;
location / {
uwsgi_pass 127.0.0.1:8000;
include /etc/nginx/uwsgi_params;
 }
}

This is the end of this article about how to configure the same domain name for the front-end and back-end of nginx. For more information about configuring the same domain name for the front-end and back-end of nginx, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to configure Nginx to distinguish between PC or mobile phone access to different domain names

<<:  Example code of how to create a collapsed header effect using only CSS

>>:  TinyEditor is a simple and easy-to-use HTML WYSIWYG editor

Recommend

A brief discussion on React native APP updates

Table of contents App Update Process Rough flow c...

In-depth analysis of MySQL execution plans

Preface In the previous interview process, when a...

How to install the graphical interface in Linux

1. Linux installation (root user operation) 1. In...

How to achieve 3D dynamic text effect with three.js

Preface Hello everyone, this is the CSS wizard - ...

Multiple ways to change the SELECT options in an HTML drop-down box

After the form is submitted, the returned HTML pag...

Detailed steps for installing MinIO on Docker

Table of contents 1. Check whether the docker env...

Introduction to ufw firewall in Linux

Let's take a look at ufw (Uncomplicated Firew...

Detailed explanation of MySQL basic operations (Part 2)

Preface This article contains 1. Several major co...

The process of building and configuring the Git environment in Docker

Configure Git environment in Docker At work, I en...

JavaScript to achieve product magnifying glass effect

This article shares the specific code of JavaScri...

Solution for adding iptables firewall policy to MySQL service

If your MySQL database is installed on a centos7 ...

Tutorial on importing and exporting Docker containers

background The popularity of Docker is closely re...

Vue custom directive details

Table of contents 1. Background 2. Local custom i...