Nginx实战部署常用功能演示(超详细版),绝对给力~~~
前言
上次分享了一些開發過程中常用的功能,但如果到真實環境中,其實還需要一些額外的配置,比如說跨域、緩存、配置SSL證書、高可用等,老規矩,還是挑幾個平時比較常用的進行演示分享。上篇詳見Nginx超詳細常用功能演示,夠用啦~~~。
正文
1. 跨域
跨域是因為瀏覽器同源策略的保護,不能直接執行或請求其他站點的腳本和數據;一般我們認為的同源就是指協議、域名、端口都相同,否則就不是同源。
現在前后端分離開發已經很普遍了,跨域問題肯定少不了,但解決的方式也很多,比如JsonP、后端添加相關請求頭等;很多時候,不想改動代碼,如果用到nginx做代理服務器,那就可以輕松配置解決跨域問題。
1.1 環境準備
需要準備兩個項目,一個前端項目發布在80端口上,一個API項目發布在5000端口上,這里需要在阿里云的安全組中將這兩個端口開放;
API項目環境(對外是5000端口)
API接口還是使用上次演示的項目,很簡單,過程我就不再重復上圖啦,直接來兩張重要的;
配置nginx反向代理,然后運行看效果:
前端環境(對外是80端口)
前端頁面(kuayu.html)
<!DOCTYPE?html> <html> <head><title>跨域測試</title><script?src="http://code.jquery.com/jquery-2.1.1.min.js"></script><script?type="text/javascript">.ready(function()¨E123E//點擊按鈕請求數據,<annotation?encoding="application style="font-size: inherit;line-height: inherit;color: rgb(136, 136, 136);overflow-wrap: inherit !important;word-break: inherit !important;" span="" encoding=""application"><span?class="katex-html"?aria-hidden="true" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span?class="strut"?style="height:1em;vertical-align:-0.25em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">(<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">d<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">o<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">c<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">u<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">m<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">n<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t).<span?class="mord?mathit"?style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">r<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">d<span?class="mord?mathit"?style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">y(<span?class="mord?mathit"?style="margin-right:0.10764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">f<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">u<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">n<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">c<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">i<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">o<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">n()¨<span?class="mord?mathit"?style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E123<span?class="mord?mathit"?style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E//<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">點<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">擊<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">按<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">鈕<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">請<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">求<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">數<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">據<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">,("#b01").click(function(){//?請求數據htmlobj=.ajax(¨E123Eurl:"http://47.113.204.41:5000/weatherforecast/getport",async:false¨E125E);//將請求的數據顯示在div中<annotation?encoding="application style="font-size: inherit;line-height: inherit;color: rgb(136, 136, 136);overflow-wrap: inherit !important;word-break: inherit !important;" span="" encoding=""application"><span?class="katex-html"?aria-hidden="true" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span?class="strut"?style="height:1em;vertical-align:-0.25em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">.<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span?class="mord?mathit"?style="margin-right:0.05724em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">j<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">x(¨<span?class="mord?mathit"?style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E123<span?class="mord?mathit"?style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">u<span?class="mord?mathit"?style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">r<span?class="mord?mathit"?style="margin-right:0.01968em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">l<span?class="mspace"?style="margin-right:0.2777777777777778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">:<span?class="mspace"?style="margin-right:0.2777777777777778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span?class="strut"?style="height:0.8888799999999999em;vertical-align:-0.19444em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">"<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">h<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">p<span?class="mspace"?style="margin-right:0.2777777777777778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">:<span?class="mspace"?style="margin-right:0.2777777777777778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span?class="strut"?style="height:1em;vertical-align:-0.25em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">//47.113.204.41<span?class="mspace"?style="margin-right:0.2777777777777778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">:<span?class="mspace"?style="margin-right:0.2777777777777778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span?class="strut"?style="height:1em;vertical-align:-0.25em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">5000/<span?class="mord?mathit"?style="margin-right:0.02691em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">w<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">h<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span?class="mord?mathit"?style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">r<span?class="mord?mathit"?style="margin-right:0.10764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">f<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">o<span?class="mord?mathit"?style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">r<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">c<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">s<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t/<span?class="mord?mathit"?style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">g<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">p<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">o<span?class="mord?mathit"?style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">r<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t",<span?class="mspace"?style="margin-right:0.16666666666666666em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">s<span?class="mord?mathit"?style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">y<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">n<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">c<span?class="mspace"?style="margin-right:0.2777777777777778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">:<span?class="mspace"?style="margin-right:0.2777777777777778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span?class="strut"?style="height:1em;vertical-align:-0.25em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span?class="mord?mathit"?style="margin-right:0.10764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">f<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span?class="mord?mathit"?style="margin-right:0.01968em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">l<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">s<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e¨<span?class="mord?mathit"?style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E125<span?class="mord?mathit"?style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E);<span?class="mspace"?style="margin-right:0.16666666666666666em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">//<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">將<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">請<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">求<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">的<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">數<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">據<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">顯<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">示<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">在<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">d<span?class="mord?mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">i<span?class="mord?mathit"?style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">v<span?class="mord?cjk_fallback" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">中("#myDiv").html(htmlobj.responseText);});});</script> </head> <body><h2>獲取結果</h2><div?id="myDiv">結果顯示</div><button?id="b01"?type="button">GetPort</button> </body> </html> </span?class="mord?cjk_fallback"></span?class="mord?mathit"?style="margin-right:0.03588em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mspace"?style="margin-right:0.16666666666666666em;"></span?class="mord?mathit"?style="margin-right:0.05764em;"></span?class="mord?mathit"?style="margin-right:0.05764em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.01968em;"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.10764em;"></span?class="strut"?style="height:1em;vertical-align:-0.25em;"></span?class="mspace"?style="margin-right:0.2777777777777778em;"></span?class="mspace"?style="margin-right:0.2777777777777778em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.03588em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mspace"?style="margin-right:0.16666666666666666em;"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.02778em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.03588em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.02778em;"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.10764em;"></span?class="mord?mathit"?style="margin-right:0.02778em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.02691em;"></span?class="strut"?style="height:1em;vertical-align:-0.25em;"></span?class="mspace"?style="margin-right:0.2777777777777778em;"></span?class="mspace"?style="margin-right:0.2777777777777778em;"></span?class="strut"?style="height:1em;vertical-align:-0.25em;"></span?class="mspace"?style="margin-right:0.2777777777777778em;"></span?class="mspace"?style="margin-right:0.2777777777777778em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="strut"?style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span?class="mspace"?style="margin-right:0.2777777777777778em;"></span?class="mspace"?style="margin-right:0.2777777777777778em;"></span?class="mord?mathit"?style="margin-right:0.01968em;"></span?class="mord?mathit"?style="margin-right:0.02778em;"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.05764em;"></span?class="mord?mathit"?style="margin-right:0.05764em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.05724em;"></span?class="mord?mathit"></span?class="strut"?style="height:1em;vertical-align:-0.25em;"></span?class="katex-html"?aria-hidden="true"></annotation?encoding="application></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?cjk_fallback"></span?class="mord?mathit"?style="margin-right:0.05764em;"></span?class="mord?mathit"?style="margin-right:0.05764em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.10764em;"></span?class="mord?mathit"?style="margin-right:0.03588em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"?style="margin-right:0.02778em;"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="mord?mathit"></span?class="strut"?style="height:1em;vertical-align:-0.25em;"></span?class="katex-html"?aria-hidden="true"></annotation?encoding="application>將kuayu.html通過xFtp拷貝到服務器上,對應的static目錄是自己創建的,如下圖:
nginx配置及運行測試
配置nginx,在原有配置文件中再新增一個server塊,如下配置:
運行測試:
跨域問題出現了,現在前后端都不想改代碼,要干架嗎?nginx說:和諧,一定要和諧~~~
1.2 配置跨域及運行
在API的server中進行跨域配置,如下:
重啟nginx之后,再測試,看看搞定了沒?
2. 配置SSL證書
現在的站點幾乎都是https了,所以這個功能必須要實操一把;為了更符合真實線上場景,我特意準備了域名和證書,真真實實在阿里云服務器上演示;這里需要登錄到阿里云上購買域名,然后根據域名申請免費的SSL證書,最后進行配置使用,詳情如下:
2.1 準備域名
這里我注冊了一個域名為:codezyq.cn;下面先說說注冊域名的流程:
登錄阿里云,找到域名注冊入口
進入到一個頁面,然后輸入需要注冊的域名
然后就出現搜索結果,如果被注冊就會提示,可以選擇其他類型或更換域名
買了域名之后,需要進行實名認證,個人上傳身份證就完事啦,一會就實名完成;?完成之后就需要配置域名解析,即解析到自己的云服務器上,后續通過域名才可以訪問;
測試是否能解析成功,直接在自己電腦上ping一下域名,看看是否解析到指定IP即可,簡單直接;這樣域名就可以用啦~~~
2.2 準備證書
免費證書這塊的申請需要用到買的域名,大概步驟如下:
領取免費證書數量(20個)
進入SSL證書(應用安全頁面)進行證書創建
進行證書申請,即綁定域名
填寫申請信息,如下:
這里一般填完申請信息,后續驗證那塊直接過也能簽發,列表狀態如下:
簽發完成之后,下載對應服務器的證書即可,點擊下載選擇
直接下載下來一會配置使用。
2.3 nginx配置證書
檢查環境
檢查端口是否可訪問
https需要443端口,所以在阿里云中將443端口加入到安全組中,如下圖:
另外還需要查看云服務器的防火墻有沒有開放對應的端口,如下命令:
#?查看防火墻端口開放情況 firewall-cmd?--list-all public target:?default icmp-block-inversion:?no interfaces:? sources:? services:?dhcpv6-client?ssh #?顯示的結果沒有開放443端口 ports:?80/tcp?22/tcp?5000/tcp?3306/tcp?6379/tcp??#?將443端口加入到里面 firewall-cmd?--zone=public?--add-port=443/tcp?--permanent #?重新加載 firewall-cmd?--reload #?再看防火墻端口開放情況 firewall-cmd?--list-all public target:?default icmp-block-inversion:?no interfaces:? sources:? services:?dhcpv6-client?ssh #?443端口加入進來了 ports:?80/tcp?22/tcp?5000/tcp?3306/tcp?6379/tcp?8080/tcp?443/tcp?檢查nginx中是否包含http_ssl_module模塊
在配置證書之前需要檢查一下nginx中是否已經包含http_ssl_module:
如果沒有就算配置了也不能用,如下:
為nginx加上http_ssl_module
需要下載一個源碼進行引入,這里還是使用版本1.18.0,具體步驟如下:
第一步,先準備環境,比如支持編譯、openssl等,執行以下命令:
#?安裝對應的包 yum?-y?install?gcc?openssl?openssl-devel?pcre-devel?zlib?zlib-devel第二步,下載對應版本源碼到usr/local/src中,并進行解壓,執行以下命令
#?下載指定版本nginx包 wget?http://nginx.org/download/nginx-1.18.0.tar.gz #?解壓 tar?-zxvf?nginx-1.18.0.tar.gz第三步,進入解壓目錄中,配置http_ssl_module,命令如下:
#?進入解壓目錄 cd?nginx-1.18.0 #?加入http_ssl_module ./configure?--prefix=/usr/local/nginx?--with-http_ssl_module第四步,編譯,在解壓目錄中直接執行make命令即可
#?執行make命令,在當前目錄就會添加新目錄objs make #?如果是新安裝nginx,執行以下命令 #?make&make?install如果沒報錯,就編譯出最新的啦;這里我在實操的時候,先執行的./configure 配置命令,然后再執行第一步的命令,所以導致make的時候老是不成功,這里解決方案是在添加http_ssl_module模塊時,同時指定了openssl源碼路徑(直接下載即可),然后再執行make命令就成功了。命令如下:
#?指定openssl源碼路徑?需要下載 ./configure?--prefix=/usr/local/nginx?--with-openssl=/usr/src/openssl-1.1.1d?--with-http_ssl_module #?再執行make命令編譯 make第五步,將編譯出來新的nginx文件替換原有的nginx文件,操作如下:
在nginx配置SSL的支持
還記得獲取下載證書的時候嗎,下載界面那有一個幫助操作,點擊就有nginx配置SSL證書的詳細步驟,如下圖:
這里演示配置的內容如下(新增一個server塊,專門配置SSL的):
server?{#?https?監聽的是443端口listen???????443?ssl;#?指定準備好的域名server_name??codezyq.cn;#?指定證書路徑,這里需要把準備好的證書放到此目錄ssl_certificate??????/usr/local/nginx/myssl/codezyq.cn.pem;ssl_certificate_key??/usr/local/nginx/myssl/codezyq.cn.key;ssl_session_cache????shared:SSL:1m;#?超時時間ssl_session_timeout?5m;#?表示使用的加密套件的類型ssl_ciphers?ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;#?表示使用的TLS協議的類型ssl_protocols?TLSv1?TLSv1.1?TLSv1.2;?ssl_prefer_server_ciphers?on;location?/www/?{root???static;index??index.html?index.htm;}}重啟nginx,然后進行https訪問對應的連接就可以啦
注:網站如果沒有備案,會被攔截,會導致不能訪問,正規站點都是要備案的。
3. 防盜鏈配置
盜鏈通俗一點的理解就是別人的網站使用自己服務器的資源,比如常見的在一個網站引入其他站點服務器的圖片等資源,這樣被盜鏈的服務器帶寬等資源就會額外被消耗,在一定程度上會損害被盜鏈方的利益,所以防盜鏈是必要的;這里通過nginx簡單實現靜態資源防盜鏈的案例,原理很簡單,就是判斷一下請求源是否被允許。
3.1 準備一個html和圖片
將準備的html和圖片放在創建的static目錄下,如下圖:
anti-stealing-link.html內容如下
<!DOCTYPE?html><html><head><title>anti-stealing-link??test</title></head><body><h2>使用云服務器上的圖片</h2><!--?訪問服務器圖片?--><img?src="http://47.113.204.41/img/test.jpg"?alt=""></body> </html>3.2 正常配置nginx
server?{listen???????80;server_name?47.113.204.41;#?針對html訪問的匹配規則location?/www/?{root?static;index?index.html?index.htm;}#?針對圖片訪問的匹配規則location?/img/?{root?static;}charset?utf-8;}運行起來看效果:
現在有個需求,只能是云服務器的html才能使用圖片,其他引用源都認為是盜鏈,直接返回403或重寫到固定圖片。
3.3 輕松配置nginx防盜鏈
針對img配置如下:
server?{listen???????80;server_name?47.113.204.41;#?針對html訪問的匹配規則location?/www/?{root?static;index?index.html?index.htm;}#?針對圖片訪問的匹配規則location?/img/?{root?static;#對源站點的驗證,驗證IP是否是47.113.204.41#可以輸入域名,多個空格隔開valid_referers?47.113.204.41;#非法引入會進入下方判斷if?($invalid_referer)?{#這里返回403,也可以rewrite到其他圖片return?403;}}charset?utf-8;}重啟nginx,清除緩存再試:
防盜鏈是不是很簡單,也可以通過代碼的形式,比如在過濾器或管道中也可以實現,如果沒有特殊需求,nginx稍微一配置就能實現,豈不美哉~~~
4. 隱藏版本信息
之前在項目中做滲透測試時,其中有一項問題就是不希望在響應頭中體現服務器相關版本,避免在某些版本出現漏洞時,攻擊者可以特意針對此版本進行惡意攻擊,從而影響系統服務可用性。
現有情況:
頁面找不到時:
看看nginx是如何關閉,如下配置:
看效果:
正常訪問
找不到報錯,版本也沒有啦
5. 高可用配置
盡管nginx性能再強,但服務器和網絡有很多因素是不可控的,如:網絡抖動、網絡不通、服務器宕機等情況,都會導致服務不可用(可理解為單點故障),這樣的系統體驗肯定得不到好評;如果當一個服務器宕機或服務掛掉時,另外一臺服務器自動代替宕機服務器提供服務,保證系統的正常運行,這就實現了高可用;這里nginx實現高可用的方式和Redis的主從模式很相似,只是nginx使用的是keepalived來實現高可用集群。
5.1 keepalived簡介
keepalived實現高可用的關鍵思路還是主、備節點的來回切換;
首先需要配置一個VIP(虛擬IP),用于提供訪問的接口地址,剛開始是在主節點上的;
當主節點發生故障時,VIP就漂移到備節點,由備節點提供服務;
如果主節點恢復,會通知備節點健康狀態,VIP就會漂移到主節點;
由上可見,在keepalive實現高可用時,肯定要有機制選擇主備節點,主備之間肯定要互相通知,不然咋知道節點之間的健康狀態,所以就使用了VRRP協議,目的就是為了解決靜態路由的單點故障。
VRRP協議,全稱Virtual Router Redundancy Protocol(虛擬路由冗余協議),利用IP多播的方式實現通信;通過競選協議機制(根據配置的優先級來競選)來將路由任務交給某臺VRRP路由器,保證服務的連續性;
理論先了解這么多,先來把keepalived安裝上,
方式一
執行以下命令可直接安裝:
#?安裝,這種直接安裝完成了,修改配置文件啟動即可 yum?install-y?keepalived #?啟動 systemctl?start?keepalived.service這種方式可能會遇到啟動keepalived的時候報錯,原因可能是服務配置文件(/usr/lib/systemd/system/keepalived.service)指定的keepalived相關目錄找不到;?如果文件目錄都正常,還報錯,我折騰了好久,后來用源碼方式進行安裝就正常啦~~~
方式二
建議使用源碼的形式進行安裝,大概步驟如下:
第一步,環境準備
#?安裝對應的依賴包 yum?-y?install?gcc?openssl?openssl-devel?pcre-devel?zlib?zlib-devel第二步,下載并解壓源碼包
#?在/usr/local/src目錄下執行,下載到該目錄 wget?https://www.keepalived.org/software/keepalived-2.0.18.tar.gz #?解壓 tar?-zxvf?keepalived-2.0.18.tar.gz第三步,安裝
#?進入到解壓出來的目錄 cd?keepalived-2.0.18 #?編譯并安裝 ./configure?&&?make?&&?make?install第四步,創建啟動文件,即將編譯出來的文件拷貝到對應的目錄下
cp??-a?/usr/local/etc/keepalived???/etc/init.d/ cp??-a?/usr/local/etc/sysconfig/keepalived????/etc/sysconfig/ cp??-a?/usr/local/sbin/keepalived????/usr/sbin/第五步,創建配置文件
#?先創建配置文件存放的目錄 mkdir?/etc/keepalived #?再將創建好的配置文件通過xFtp傳到此目錄,也可以直接在這里創建配置文件名為keepalived.conf,內容如下:
!?Configuration?File?for?keepalived global_defs?{#?每臺機器的唯一標識router_id?31 } #?vrrp實例,可以配置多個 vrrp_instance?VI_1?{#?標識為主節點,如果是被節點,就為BACKUPstate?MASTER#?網卡名稱,通過ip?addr?命令可以看到對應網卡,需要哪個就配置哪個就行interface?enp0s8#?路由ID,主、備節點的id要一樣virtual_router_id?3#?優先級,主節點的優先級要大于備節點的優先級priority?200#?主備心跳檢測,間隔時間為1sadvert_int?1?#?認證方式,主備節點要一致authentication?{auth_type?PASSauth_pass?123456}virtual_ipaddress?{#?虛擬IP192.168.30.108} }第六步,啟動
#?啟動 systemctl?start?keepalived.service #?查看虛擬IP情況 ip?addr?查看效果如下,虛擬ip正常在master節點上:
安裝完keepalived和nginx之后就可以進行主備演示啦~~~
5.2 主備演示
這里用了兩臺虛擬機,結構如下:
第一步nginx配置準備
這里用到的就是nginx默認配置,基本沒咋改,如果要了解配置文件詳情,點擊(Nginx超詳細常用功能演示,夠用啦~~~)有詳細說明。兩臺虛擬機配置的nginx.conf內容如下:
#user??nobody; worker_processes??1; events?{worker_connections??1024; } http?{include???????mime.types;default_type??application/octet-stream;sendfile????????on;keepalive_timeout??65;#gzip??on;server?{#?需要防火墻開放80端口listen???????80;server_name??localhost;location?/?{root???html;index??index.html?index.htm;}error_page???500?502?503?504??/50x.html;location?=?/50x.html?{root???html;}} }第二步準備html
兩臺虛擬機中html,用的是nginx默認的index.html(路徑:/usr/local/nginx/html),為了方便演示,在index.html中增加了個105和106的顯示標注,105機器內容如下,106機器上的html只是把內容中的105改成106即可:
第三步準備兩臺虛擬機上keepalived配置文件(路徑:/etc/keepalived)
105機器上keepalived.conf內容如下:
!?Configuration?File?for?keepalived global_defs?{#?每臺機器不一樣router_id?31 } #檢測nginx服務是否在運行 vrrp_script?chk_nginx?{#使用腳本檢測script?"/usr/local/src/chk_nginx.sh"#腳本執行間隔,每2s檢測一次interval?2#腳本結果導致的優先級變更,檢測失敗(腳本返回非0)則優先級?-5weight?-5#連續2次檢測失敗才確定是真失敗fall?2#檢測到1次成功就算成功rise?1???????????????????? }vrrp_instance?VI_1?{#?vrrp實例,可以配置多個state?MASTER#?網卡名稱,通過ip?addr?命令可以看到對應網卡,需要哪個就配置哪個就行interface?enp0s8#?路由ID,主、備節點的id要一樣virtual_router_id?3#?優先級,主節點的優先級要大于備節點的優先級priority?200#?主備心跳檢測,間隔時間為1sadvert_int?1#?認證方式,主備節點要一致authentication?{auth_type?PASSauth_pass?123456}#執行監控的服務。track_script?{?#引用VRRP腳本,即在 vrrp_script 部分指定的名字。chk_nginx????????????????????}virtual_ipaddress?{#?虛擬IP192.168.30.108} }在/usr/local/src/中準備chk_nginx.sh,keepalived和nginx沒有直接關系的,只有通過檢查nginx的運行狀態來進行高可用服務切換,chk_nginx.sh內容如下:
然后給這個文件增加執行權限,命令如下:
chmod?+x?chk_nginx.sh106虛擬機上的步驟和檢測腳本chk_nginx.sh都一樣,只是keepalived.conf內容稍微有點變動。
106機器上keepalived.conf內容如下:
!?Configuration?File?for?keepalived global_defs?{#?每臺機器上不一樣router_id?32 } #檢測nginx服務是否在運行 vrrp_script?chk_nginx?{#使用腳本檢測script?"/usr/local/src/chk_nginx.sh"#腳本執行間隔,每2s檢測一次interval?2#腳本結果導致的優先級變更,檢測失敗(腳本返回非0)則優先級?-5weight?-5#連續2次檢測失敗才確定是真失敗fall?2#檢測到1次成功就算成功rise?1???????????????????? }? vrrp_instance?VI_1?{#?配置為備節點state?BACKUP#?ip?addr?查看對應的網卡名稱interface?enp0s8virtual_router_id?3#?優先級比主節點低priority?100advert_int?1?authentication?{auth_type?PASSauth_pass?123456}#執行監控的服務track_script?{#引用VRRP腳本,即在 vrrp_script 部分指定的名字。chk_nginx????????????????????}virtual_ipaddress?{#?虛擬IP192.168.30.108} }第四步分別啟動兩臺虛擬機上的nginx和keepalived,命令如下:
#?啟動nginx cd?/usr/local/nginx/sbin/ ./nginx #?啟動keepalived systemctl?start?keepalived.service #?查看keepalived狀態,是否運行 systemctl?status?keepalived.service兩臺虛擬機都要執行
第五步測試,效果如下:
直接訪問虛擬IP就能訪問到主節點的服務啦;現在測試當主節點掛掉時,還會不會正常訪問服務,在105機器上執行如下命令:
#?模擬宕機,停止keepalived systemctl?stop?keepalived.service #?ip?addr?查看虛擬IP已經漂移到備節點上了,在備節點用ip?addr?查看備節點顯示,虛擬IP已經漂移過來啦~
再用虛擬IP訪問,效果如下:
看見已經切換到106機器上提供服務啦,這樣就實現高可用啦;
那主節點重新恢復,虛擬IP會不會恢復回來繼續提供服務呢?
#?重啟keepalived systemctl?restart?keepalived.service #ip?addr?查看虛擬IP情況,恢復過來啦繼續用虛擬IP訪問服務,又回到主服務啦,如果沒有,那可能是瀏覽器緩存,因為這里用靜態html演示,清掉緩存再訪問。
5.3 多主多備演示
對于上面的主備模式,只有主節點提供服務,如果主節點不發生故障,備節點服務器就有點資源浪費啦,這個時候多主多備不僅能合理利用資源,還得提供備用服務(根據實際需要配置),形成真正集群提供服務。
這里就演示一下雙主雙備的配置,思路就是在keepalived增加多個vrrp實例,105機器在vrrp實例VI_1中作為主節點,106機器作為備節點,在vrrp實例VI_2中,105機器作為備節點,106機器作為主節點,這樣就形成了互為主備的模式,資源就能很好的利用啦;其他邏輯不變,只是分別在105、106機器上加上的keepalived.conf中加上VI_2實例即可,如下:
105機器上keepalived.conf內容如下:
global_defs?{router_id?31 } #檢測nginx服務是否在運行 vrrp_script?chk_nginx?{#使用腳本檢測script?"/usr/local/src/chk_nginx.sh"#腳本執行間隔,每2s檢測一次interval?2#腳本結果導致的優先級變更,檢測失敗(腳本返回非0)則優先級?-5weight?-5#連續2次檢測失敗才確定是真失敗fall?2#檢測到1次成功就算成功rise?1??????????????????? }vrrp_instance?VI_1?{state?MASTERinterface?enp0s8virtual_router_id?3priority?200advert_int?1?authentication?{auth_type?PASSauth_pass?123456}track_script?{??????????????????????chk_nginx????????????????????}virtual_ipaddress?{192.168.30.108} } vrrp_instance?VI_2?{#?VI_1是MASTER,這里就是備節點state?BACKUPinterface?enp0s8#?修改路由編號virtual_router_id?5#?備節點優先級稍低priority?100advert_int?1authentication?{auth_type?PASSauth_pass?123456}track_script?{??????????????????????chk_nginx????????????????????}virtual_ipaddress?{#?虛擬IP192.168.30.109} }106機器上keepalived.conf內容如下:
global_defs?{router_id?32 } #檢測nginx服務是否在運行 vrrp_script?chk_nginx?{#使用腳本檢測script?"/usr/local/src/chk_nginx.sh"#腳本執行間隔,每2s檢測一次interval?2#腳本結果導致的優先級變更,檢測失敗(腳本返回非0)則優先級?-5weight?-5#連續2次檢測失敗才確定是真失敗fall?2#檢測到1次成功就算成功rise?1??????????????????? } vrrp_instance?VI_1?{state?BACKUPinterface?enp0s8virtual_router_id?3priority?100advert_int?1?authentication?{auth_type?PASSauth_pass?123456}track_script?{??????????????????????chk_nginx????????????????????}virtual_ipaddress?{192.168.30.108} } vrrp_instance?VI_2?{#?這里是主節點state?MASTERinterface?enp0s8#?這里和105機器上的VI_2中id一致virtual_router_id?5priority?200advert_int?1authentication?{auth_type?PASSauth_pass?123456}track_script?{??????????????????????chk_nginx????????????????????}virtual_ipaddress?{#?虛擬IP192.168.30.109} }分別重啟兩臺機器上的keepalived,執行命令如下:
然后分別訪問虛擬ip 192.168.30.108 和192.168.30.109,都能提供對應的服務啦,這里不截圖了。
總結
開發和生產環境比較常用的功能大概就這么多,后續如果用得多的功能會及時和小伙伴分享哦。
這次主要遇見一些問題,小伙伴可以作為參考,匯總如下:
VMware虛擬機網絡問題,主機ping不通虛擬機、虛擬機ping不通主機、上不了外網
這個問題折騰了兩個晚上,原因是之前的網絡配置有一些問題,然后重新裝了虛擬機,但是用單網卡的方式依然沒有同時解決上面的問題,最后使用兩個網卡的方式解決了;一個網卡使用動態分配,另外一個使用靜態IP,信息如下:
keepalived啟動不起來
使用yum直接安裝的方式,可能會遇到啟動不起來的情況,排查了服務里面配置路徑,目錄都對,依賴包也裝了還不行;最后使用源碼方式安裝沒問題。
keepalived的虛擬ip主機ping不通
這也是個棘手的問題,虛擬ip能在主、備機器之間正常漂移,就是主機ping不同虛擬IP,當前解決的措施主要步驟,如下:
首先注釋掉keepalived.conf 中vrrp_strict ,然后重啟keepalived;
如果還不行,就排查防火墻;
再不行,就清arp緩存
一個被程序搞丑的帥小伙,關注"Code綜藝圈",跟我一起學~~~
總結
以上是生活随笔為你收集整理的Nginx实战部署常用功能演示(超详细版),绝对给力~~~的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎能错过这个技术集市!转发有奖,惊喜连连
- 下一篇: Dotnet创建Linux下的Servi