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

Detailed explanation of soft links and hard links in Linux

Table of contents 1. Basic storage of files and d...

Linux yum package management method

Introduction yum (Yellow dog Updater, Modified) i...

Chinese website user experience rankings

<br />User experience is increasingly valued...

Web Design Help: Web Font Size Data Reference

<br />The content is reproduced from the Int...

How to deploy Go web applications using Docker

Table of contents Why do we need Docker? Docker d...

Vue project realizes paging effect

The paging effect is implemented in the vue proje...

BUG of odd width and height in IE6

As shown in the figure: But when viewed under IE6...

vue-amap installation and usage steps

I have previously shared the usage of asynchronou...

How to convert MySQL horizontally to vertically and vertically to horizontally

Initialize Data DROP TABLE IF EXISTS `test_01`; C...

How to display div on object without being blocked by object animation

Today I made a menu button. When you move the mous...