您好,登錄后才能下訂單哦!
小編給大家分享一下如何實現nginx前后端同域名配置,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
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 }
前后端分離的項目,前端和后端可以用不同的域名,也可以用相同的域名
以下為前后端使用相同域名情況:
一、前端使用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前后端同域名配置”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。