欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

nginx前后端同域名配置的方法实现

程序员文章站 2022-03-19 09:11:39
本文主要介绍了nginx前后端同域名配置的方法实现,分享给大家,具体如下:upstream dfct {# ip_hash; server 121.41.19.236:8192;} server {...

本文主要介绍了nginx前后端同域名配置的方法实现,分享给大家,具体如下:

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
 
 
}

补充:前后端分离的项目使用nginx部署的三种方式

前后端分离的项目,前端和后端可以用不同的域名,也可以用相同的域名

以下为前后端使用相同域名情况:

一、前端使用www.xxx.com,后端使用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;
 }
}

二、前端使用www.xxx.com,后端使用www.xxx.com/api/

1、uwsgi如果是使用http方式可以这样配

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、uwsgi如果是使用socket方式的话需要这样配

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;
 }
}

到此这篇关于nginx前后端同域名配置的方法实现的文章就介绍到这了,更多相关nginx前后端同域名配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!