nginx前后端同域名配置的方法實現(xiàn)
發(fā)布日期:2021-12-23 03:03 | 文章來源:gibhub
本文主要介紹了nginx前后端同域名配置的方法實現(xiàn),分享給大家,具體如下:
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前后端同域名配置的方法實現(xiàn)的文章就介紹到這了,更多相關nginx前后端同域名配置內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!
版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
相關文章