Nginx快速入門
【一句話總結】Nginx快速入門:在一個新安裝的Linux系統上安裝並使用Nginx。
一、Nginx的使用場景:通過配置,使用域名即可訪問服務
我有一個專案。其有多個微服務模組,部署在同一個主機上,訪問地址分別是:
192.168.0.1:801【後臺管理服務admin.myhome.com】
192.168.0.1:18001【圖片服務img.myhome.com】
192.168.0.1:18002【音訊服務music.myhome.com】
一般來說,我們通過ip:port即可訪問該服務,但是,這裡希望可以只輸入域名即可訪問。 比如admin.myhome.com 就進入後臺管理。
附:傳統的訪問方式,【域名:埠】,配置流程如下:
1.修改本機hosts檔案 192.168.0.1 admin.myhome.com img.myhome.com music.myhome.com 這樣,當訪問這三個域名時都會對映到該ip
2.域名解析與訪問
admin.myhome.com:801 會被解析為192.168.0.1:801,即可訪問後臺管理系統
img.myhome.com:18001 會被解析為192.168.0.1:18001,即可訪問圖片服務
music.myhome.com:18002 會被解析為192.168.0.1:18002,即可訪問音訊服務【當然,如果不在hosts檔案裡寫這些域名對應的ip,那麼它就會通過本機的DNS域名解析在網路上尋找對應域名並訪問。hosts檔案裡的localhost或127.0.0.1都是代表本機】
為什麼要使用反向代理:專案一般都有開發、生產、預釋出等多個環境,如果不同環境使用不同的ip去訪問,可能會出現一些問題。
為了保證所有環境的一致,我們會在各種環境下都使用域名來訪問。
二、Nginx的使用:
1安裝
1)安裝其執行環境:gcc、pcre-devel、zlib-devel、openssl-devel
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel 一次性把這幾個環境都安裝完畢 -y則不再需要去手動確定,都選擇預設安裝即可
2)下載:Nginx[以1.8.1為例]: https://nginx.org/download/
3)拷貝下載的壓縮檔案到指定位置
將下載好的“nginx-1.8.1.tar.gz”壓縮包,複製到/usr/local/下,我個人一般軟體都安裝在這個目錄下。
這裡或許需要先開啟windows和linux的互相複製的功能,請自行百度。
4)解壓:
開啟終端面板,先使用【cd /urs/local】再使用指令【tar -zxvf nginx-1.8.1.tar.gz】解壓縮。
此時local資料夾下有該壓縮包,還有解壓後的檔案
5)配置:
先用【cd nginx-1.8.1】進入解壓後的資料夾,再用【./configure --prefix=/usr/local/nginx】配置
6)編譯和安裝:
先執行【make】,再執行【make install】,則此時已安裝完成,local資料夾下多了一個【nginx資料夾,即安裝好的nginx】
這裡,可以用小寫的LS或LL來檢視當前資料夾下都有什麼。
安裝完之後usr/local下有【nginx-1.8.1.tar.gz】【nginx-1.8.1】 【nginx】三個資料夾,前兩個都可以刪除。
7)嘗試啟動:【cd usr/local/nginx/sbin】到sbin目錄 ,再用【./nginx】來啟動
這裡,可以先用【ps -A | grep nginx】檢視Nginx是否啟動 有返回結果則是已經啟動的,避免意外,需殺掉程序重啟nginx。
然後,【lsof -i:80】檢視80端口占用 【kill -9 埠號】 關閉此埠
8)測試:
在Linux中啟動瀏覽器,並訪問localhost,即可訪問【localhost:80】,則會彈出Ningx的歡迎介面
9)Nginx安裝使用的常用命令
【./nginx -s reload】重啟nginx 【/usr/local/nginx/sbin/nginx】在任意目錄下時啟動Nginx
【./nginx】在sbin目錄下時啟動nginx 【ps -A | grep nginx】檢視Nginx是否啟動 有返回結果則是已經啟動的
【lsof -i:80】檢視80端口占用 【kill -9 埠號】 關閉此埠
2存在的問題處理-這三個都必須自查一遍
1)安裝時未自動生成logs資料夾
報【could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)】錯
則【cd /usr/loca/nginx/】到nginx目錄下 【mkdir logs】建立日誌資料夾 【chmod 700 logs】設定該資料夾logs的讀寫許可權
2)防火牆禁止了80埠
解決方案一:先永久開放80埠【firewall-cmd --permanent --add-port=80/tcp】
再重啟防火牆【firewall-cmd --reload】
解決方案二:用【vi /etc/sysconfig/iptables】開啟ip埠配置檔案,在裡面新增開放80埠的設定
【-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT】
可能問題:啟動防火牆時出現Unit is masked錯誤,則在這取消防火牆鎖定【systemctl unmask firewalld】-->
而後期如有需要用【systemctl mask firewalld】鎖定防火牆
常用指令:systemctl status firewalld檢視防火牆狀態 systemctl start firewalld開啟防火牆
systemctl stop firewalld關閉防火牆
3)SELinux配置問題
解決方案一:SELinux配置將http網路連線關閉,啟用即可【setsebool -P httpd_can_network_connect 1】 參考網
解決方案二:直接關閉SELiunx配置 進入【vi/etc/selinux/config】 修改【SELINUX=disabled】 參考網
3和專案聯合使用
1)配置Nginx
【cd /usr/local/nginx/conf】進入配置資料夾 --->開啟【nginx.conf】檔案 ---> 新增配置
1 #user nobody; 2 worker_processes 1; 3 4 #error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 8 #pid logs/nginx.pid; 9 10 11 events { 12 worker_connections 1024; 13 } 14 15 16 http { 17 include mime.types; 18 default_type application/octet-stream; 19 20 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 21 # '$status $body_bytes_sent "$http_referer" ' 22 # '"$http_user_agent" "$http_x_forwarded_for"'; 23 24 #access_log logs/access.log main; 25 26 sendfile on; 27 #tcp_nopush on; 28 29 #keepalive_timeout 0; 30 keepalive_timeout 65; 31 32 #gzip on; 33 34 # 後端管理的代理 35 server { 36 listen 80; 37 server_name www.admin.myhome.com; 38 39 #charset koi8-r; 40 41 #access_log logs/host.access.log main; 42 43 location / { 44 proxy_pass http://192.168.0.1:801; 45 } 46 } 47 48 # 圖片服務的代理 49 server { 50 listen 80; 51 server_name img.myhome.com; 52 53 #charset koi8-r; 54 55 #access_log logs/host.access.log main; 56 57 location / { 58 proxy_pass http://192.168.0.1:18001; 59 } 60 } 61 62 # 音訊服務的代理 63 server { 64 listen 80; 65 server_name music.myhome.com; 66 67 #charset koi8-r; 68 69 #access_log logs/host.access.log main; 70 71 location / { 72 proxy_pass http://192.168.0.1:18002; 73 } 74 } 75 76 # 初始的代理 localhost 有這個nginx才能訪問自己主頁 不然就會404 77 server { 78 listen 80; 79 server_name localhost; 80 81 #charset koi8-r; 82 #access_log logs/host.access.log main; 83 84 location / { 85 root html; #這裡也可以新增一個自定義的Nginx主頁 用路徑即可 86 index index.html index.htm; 87 } 88 #error_page 404頁面的處理 /404.html; 89 # redirect server error pages to the static page /50x.html 90 error_page 500 502 503 504 /50x.html; 91 location = /50x.html { 92 root html; 93 } 94 } 95 96 }Nginx的代理配置nginx.conf
2)Nginx反向代理的示意圖
附:第二種下載安裝方式:使用wegt下載nginx
附:本文參考:安裝Nginx參考