Realization of Configuration Method of Domain Name in Front and Back Ends of nginx

  • 2021-10-25 08:18:56
  • OfStack

This paper mainly introduces the method of configuring the same domain name at the front and back ends of nginx, and shares it with everyone, 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
 
 
}

Add: Three ways to deploy nginx for projects with separate front and back ends

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

The following is the case where the front and back ends use the same domain name:

1. www. xxx. com for the front end and api. xxx. com for the back end


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. uwsgi can be matched in this way if http is used


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 is used in socket mode, it needs to be matched 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;
 }
}


Related articles: