華爲雲CentOS 7.4 安裝Nginx1.15.8
4.1 前提
Nginx官網:http://nginx.org/
由於Nginx模塊依賴一些類庫,所以在安裝Nginx之前,必須先安裝這些lib庫,主要有如下幾個安裝命令,安裝過程的提示命令都直接輸入【y】繼續安裝即可。
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
4.2 安裝Nginx
4.2.1 下載
wget http://nginx.org/download/nginx-1.15.8.tar.gz
4.2.2 解壓文件
tar -zxvf nginx-1.15.8.tar.gz
4.2.3 安裝
./configure && make && make install
注意:默認安裝在/usr/local/nginx, 使用–prefix參數指定nginx安裝的目錄。可以使用命令【whereis nginx】來查看安裝目錄。
4.3 啓動
安裝完畢後,進入安裝後目錄(/usr/local/nginx)便可以啓動或停止。
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s stop
設置開啓啓動,文件末尾添加啓動腳本(/usr/local/nginx/sbin/nginx):
chmod 755 /etc/rc.d/rc.local
vim /etc/rc.d/rc.local
4.4 注意事項
- 華爲雲ECS服務器默認不開啓80端口,直接訪問存在問題。需要進入華爲官網雲控制檯中安全組添加(入規則:80)。
- 華爲雲ECS默認開啓了防火牆,linux防火牆禁止訪問80端口。
確認Nginx已經啓動並監聽了80端口
[root@ecs-s6 home]# netstat -anp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11309/nginx: master
確認Linux防火牆禁止訪問80端口。
[root@ecs-s6 logs]# firewall-cmd --query-port=80/tcp
no
解決方式:
# 1 防火牆列表中允許80端口
firewall-cmd --permanent --add-port=80/tcp
# 修改配置後重新啓動才生效
firewall-cmd --reload
# 2 關閉防火牆
systemctl stop firewalld
上述設定後再次訪問Nginx地址,可以顯示歡迎頁,訪問OK。
4.5 反向代理配置
反向代理(Reverse Proxy)方式是指Nginx以代理服務器來接受網絡上的連接請求,然後將請求轉發給內部網絡上的服務器,並將從內部服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現爲一個反向代理服務器。當外部客戶端嘗試訪問內容服務器時,會將其送到代理服務器。實際內容位於內容服務器上,在防火牆內部受到安全保護,所以說反向代理服務器也起到了安全防護作用。反向代理服務器還可以實現負載均衡。
4.5.1 VUE靜態頁面部署
我們在服務器上部署一個前端的VUE應用(其實就是找一個目錄,拷貝代碼即可),現在主要是靜態頁面,當我們訪問這個機器的指定端口時,希望能夠看到這個默認的靜態頁面。
VUE應用部署:/home/meetmng/vueweb
[root@ecs-s6 vueweb]# ll
total 12
-rw-r--r-- 1 root root 922 Jan 13 15:19 meetingDetail.html
-rw-r--r-- 1 root root 910 Jan 13 15:18 meeting.html
drwxr-xr-x 9 root root 4096 Jan 13 15:19 static
[root@ecs-s6 vueweb]#
4.5.2 nginx.conf配置轉發
打開Nginx安裝目錄下面的配置文件:/usr/local/nginx/conf/nginx.conf
我們這個應用比較簡單,默認首頁訪問地址基本修改內容如下:
# 原來的默認內容
location / {
root html;
index index.html index.htm;
}
# 修改後內容
location / {
root /home/meetmng/vueweb;
index meeting.html;
}
前端VUE畫面中調用SpringBoot的RestFul接口配置如下:
# 添加接口的映射內容
location /gavin/api/confmng {
proxy_pass http://localhost:9109/gavin/api/confmng;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
按照如上設定後,重新啓動Nginx,訪問頁面可以看到靜態頁面能夠正常加載。
4.6 nginx.conf配置文件說明
## 運行用戶和用戶組,默認關閉,如果沒有用戶可以指定root
#user nobody;
## 啓動進程,通常設定和cpu核數相等
worker_processes 1;
## 全局錯誤日誌,可以指定錯誤級別
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
## PID文件位置,在worker_processes>1 的情況下會有多個進程,
## 但是管理進程只有一個
#pid logs/nginx.pid;
events {
## 每個進程可同時建立的鏈接數量
worker_connections 1024;
}
http {
## ========Nginx後端服務配置項 Start =========
location /gavin/api/confmng {
proxy_pass http://localhost:9109/gavin/api/confmng;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
## ========Nginx後端服務配置項 End =========
# 文件擴展名與文件類型映射表
include mime.types;
# 默認文件類型
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
# 開啓高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件,
# 對於普通應用設爲 on,如果用來進行下載等應用磁盤IO重負載應用,
# 可設置爲off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。
sendfile on;
# 防止網絡阻塞
#tcp_nopush on;
# 長連接超時時間,單位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
# 監聽端口
listen 80;
# 域名可以有多個,用空格隔開
server_name localhost;
# 編碼格式
#charset koi8-r;
#access_log logs/host.access.log main;
# 對 "/" 啓用反向代理
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}