Nginx是一款輕量級的Web服務器/反向代理服務器及電子郵件 (IMAP/POP3) 代理服務器,並在一個BSD-like協議下發行.其特色是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等.html
1.配置Yum倉庫,安裝Nginx所依賴的包文件,以及編譯器.nginx
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@localhost ~]# yum -y install epel-release [root@localhost ~]# yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.編譯安裝Nginx.web
[root@localhost ~]# useradd -s /sbin/nologin -M nginx [root@localhost ~]# wget http://nginx.org/download/nginx-1.13.12.tar.gz [root@localhost ~]# tar -xzvf nginx-1.13.12.tar.gz [root@localhost ~]# cd nginx-1.13.12/ [root@localhost ~]# ./configure --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_stub_status_module [root@localhost ~]# make && make install
3.檢查Nginx配置文件正確性,啓動關閉與重啓Nginx配置.算法
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t #檢測配置文件正確性 [root@localhost ~]# /usr/local/nginx/sbin/nginx #啓動Nginx [root@localhost ~]# kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid) #關閉Nginx [root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid) #重啓Nginx
做用:當咱們打開指定網頁時,會提示須要輸入密碼才能訪問,這就是密碼認證技術.chrome
1.編輯Nginx主配置文件,在相應的區域中加入如下標★語句.vim
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 41 #access_log logs/host.access.log main; 42 43 location / { #對根站點,設置密碼認證 44 root html; #主頁位置,相對路徑 45 index index.html index.htm; #主頁執行順序 ★ auth_basic "welcome to admin"; #提示信息(自定義) ★ auth_basic_user_file /usr/local/nginx/html/login.pad; #生成的密碼文件 46 } 47 48 #error_page 404 /404.html; 49 50 # redirect server error pages to the static page /50x.html
2.因爲Nginx沒有生成密碼文件的工具,這裏須要藉助Apache的工具生成密碼文件.後端
[root@localhost ~]# yum install -y httpd [root@localhost ~]# htpasswd -c /usr/local/nginx/html/login.pad lyshark #建立認證用戶(覆蓋) [root@localhost ~]# htpasswd -m /usr/local/nginx/html/login.pad lyshark #寫入認證用戶(追加)
3.重啓Nginx服務,訪問指定的頁面,便可實現認證.緩存
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
做用:當咱們打開指定網頁時,會判斷您的IP地址是容許訪問仍是拒絕訪問,這就是基於IP的認證技術安全
1.編輯Nginx主配置文件,在相應的區域中加入如下任意標★語句,具體狀況具體對待bash
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 41 #access_log logs/host.access.log main; 42 43 location / { 44 root html; 45 index index.html index.htm; ★ allow 192.168.1.10; #容許單個IP訪問 ★ deny 192.168.1.10; #拒絕單個IP訪問 ★ allow 0.0.0.0/0; #容許全部網段 ★ deny 0.0.0.0/0; #拒絕全部網段 46 } 47 48 #error_page 404 /404.html; 49 50 # redirect server error pages to the static page /50x.html
2.重啓Nginx服務,訪問指定的頁面,便可實現認證.
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
利用虛擬主機功能,能夠把一臺處於運行狀態的物理服務器分割成多個,虛擬服務器,出於各類考慮目前各類企業都在使用虛擬主機功能,Nginx虛擬主機功能,是服務器基於用戶的請求的不一樣Ip地址,主機域名或端口號,實現提供多個網站同時爲外部提供訪問服務的技術,用戶取得的資源不一樣最後取得的頁面也會不一樣.
若是一臺服務器有多個IP地址,並且每一個IP地址與服務器上部署的每一個網站對應,這樣當用戶請求訪問不一樣的IP時,會訪問到不一樣網站的頁面資源,並且每一個網站都有一個獨立的IP地址,如下實驗將實如今一臺服務器上配置多個IP,搭建多個網站,每一個網站使用一個IP地址.
1.在eno16777728上配置一個網卡子接口.
[root@localhost ~]# ifconfig eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::20c:29ff:fe1e:14e2 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:1e:14:e2 txqueuelen 1000 (Ethernet) RX packets 40292 bytes 4129804 (3.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8962 bytes 1557264 (1.4 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno16777728:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.20 netmask 255.255.255.0 broadcast 192.168.1.255 ether 00:0c:29:1e:14:e2 txqueuelen 1000 (Ethernet)
2.編輯主配置文件,分別複製並編輯兩個主機區域,在相應的區域中加入如下標★語句
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 35 server { ★ listen 192.168.1.10:80; #指定區域1的IP地址 37 server_name localhost; 38 39 location / { ★ root html/vhost1; #指定區域1的文件目錄 41 index index.html index.htm; 42 } 43 44 error_page 500 502 503 504 /50x.html; 45 location = /50x.html { 46 root html; 47 } 48 } 49 server { ★ listen 192.168.1.20:80; #指定區域2的IP地址 51 server_name localhost; 52 53 location / { ★ root html/vhost2; #指定區域2的文件目錄 55 index index.html index.htm; 56 } 57 58 error_page 500 502 503 504 /50x.html; 59 location = /50x.html { 60 root html; 61 } 62 }
3.在html目錄下建立相應目錄以及html文件.
[root@localhost ~]# mkdir /usr/local/nginx/html/vhost1/ [root@localhost ~]# mkdir /usr/local/nginx/html/vhost2/ [root@localhost ~]# echo "ip 1 server" > /usr/local/nginx/html/vhost1/index.html [root@localhost ~]# echo "ip 2 server" > /usr/local/nginx/html/vhost2/index.html
4.重啓Nginx服務,此時訪問不一樣的IP會出現不一樣的頁面
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
基於端口的虛擬主機,可讓用戶經過端口號,來訪問服務器上的資源,在使用Nginx配置虛擬網站時,基於端口的配置方式最爲複雜,如下實驗將實如今一臺服務器上配置多個端口,搭建多個網站,每一個網站使用一個端口.
1.編輯主配置文件,分別複製並編輯兩個主機區域,在相應的區域中加入如下標★語句.
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 35 server { #主機區域1(server1) ★ listen 80; #主機1端口 ★ server_name localhost; #主機1服務器名(域名1) 38 39 location / { ★ root html/lyshark-80; #域名1網頁存放位置 41 index index.html index.htm; 42 } 43 error_page 500 502 503 504 /50x.html; 44 location = /50x.html { 45 root html; 46 } 47 } 48 server { #主機區域2(server2) ★ listen 8080; #主機區域2端口 ★ server_name localhost; #主機2服務器名(域名2) 51 52 location / { ★ root html/lyshark-8080; #域名2網頁存放位置 54 index index.html index.htm; 55 } 56 57 error_page 500 502 503 504 /50x.html; 58 location = /50x.html { 59 root html; 60 } 61 }
2.在html目錄下建立相應目錄以及html文件.
[root@localhost ~]# mkdir /usr/local/nginx/html/vhost-80/ [root@localhost ~]# mkdir /usr/local/nginx/html/vhost-8080/ [root@localhost ~]# echo "80 server zone" > /usr/local/nginx/html/vhost-80/index.html [root@localhost ~]# echo "8080 server zone" > /usr/local/nginx/html/vhost-8080/index.html
3.重啓Nginx服務,此時訪問同一網站的不一樣端口,會有不一樣頁面.
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
當服務器沒法爲每個網站分配一個獨立的IP的時候,能夠嘗試讓Nginx自動識別用戶請求的域名,從而根據不一樣的域名請求來傳輸不一樣的內容,這裏咱們爲了驗證明驗要手動搭建一個DNS解析,如下實驗將實如今一臺服務器上多個域名,搭建多個網站,每一個網站使用一個域名.
1.首先搭建DNS域名解析,模擬vhost1.com與vhost2.com兩個網站域名.
[root@localhost ~]# yum install -y bind bind-chroot Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager. Package 32:bind-9.9.4-61.el7.x86_64 already installed and latest version Package 32:bind-chroot-9.9.4-61.el7.x86_64 already installed and latest version Nothing to do
2.配置DNS解析,這裏咱們簡單配置便可,有關DNS詳細例子請查看其餘相關文章.
[root@localhost ~]# vim /etc/named.conf 12 options { 13 listen-on port 53 { any; }; 14 listen-on-v6 port 53 { ::1; }; 15 directory "/var/named"; 16 dump-file "/var/named/data/cache_dump.db"; 17 statistics-file "/var/named/data/named_stats.txt"; 18 memstatistics-file "/var/named/data/named_mem_stats.txt"; 19 allow-query { any; }; [root@localhost ~]# vim /etc/named.rfc1912.zones 43 zone "vhost1.com" IN { 44 type master; 45 file "vhost1.com.zone"; 46 allow-update { none; }; 47 }; 48 zone "vhost2.com" IN { 49 type master; 50 file "vhost2.com.zone"; 51 allow-update { none; }; 52 };
3.拷貝配置文件,並修改爲如下模樣,並重啓Bind.
[root@localhost ~]# cp -a /var/named/named.localhost /var/named/vhost1.com.zone [root@localhost ~]# cp -a /var/named/named.localhost /var/named/vhost2.com.zone [root@localhost ~]# vim /var/named/vhost1.com.zone $TTL 1D @ IN SOA dns.vhost1.com. rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS dns.vhost1.com. dns A 127.0.0.1 www A 192.168.1.10 [root@localhost ~]# vim /var/named/vhost2.com.zone $TTL 1D @ IN SOA dns.vhost2.com. rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS dns.vhost2.com. dns A 127.0.0.1 www A 192.168.1.10 [root@localhost ~]# systemctl restart named
4.編輯主配置文件,分別複製並編輯兩個主機區域,在相應的區域中加入如下標★語句.
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 35 server { #主機區域1(server1) 36 listen 80; ★ server_name www.vhost1.com; #主機1服務器名(域名1) 38 39 location / { ★ root html/vhost1; #域名1網頁存放位置 41 index index.html index.htm; 42 } 43 error_page 500 502 503 504 /50x.html; 44 location = /50x.html { 45 root html; 46 } 47 } 48 server { #主機區域2(server2) 49 listen 80; ★ server_name www.vhost2.com; #主機2服務器名(域名2) 51 52 location / { ★ root html/vhost2; #域名2網頁存放位置 54 index index.html index.htm; 55 } 56 57 error_page 500 502 503 504 /50x.html; 58 location = /50x.html { 59 root html; 60 } 61 }
2.在html目錄下建立相應目錄以及html文件.
[root@localhost ~]# mkdir /usr/local/nginx/html/vhost1/ [root@localhost ~]# mkdir /usr/local/nginx/html/vhost2/ [root@localhost ~]# echo "vhost1 server zone" > /usr/local/nginx/html/vhost1/index.html [root@localhost ~]# echo "vhost2 server zone" > /usr/local/nginx/html/vhost2/index.html
3.重啓Nginx服務,此時訪問兩個域名,出現兩個網站之間不衝突.
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
做用:當用戶訪問本臺Nginx代理服務器時,會自動跳轉到代理的地址上面.
1.修改Nginx主配置文件,在相應的區域中加入如下標★語句.
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 41 #access_log logs/host.access.log main; 42 43 location / { 44 root html; #註釋掉 45 index index.html index.htm; #註釋掉 ★ proxy_pass http://192.168.1.100; #反向代理,當有人訪問根時,自動轉到100上 46 } 47 48 #error_page 404 /404.html; 49 50 # redirect server error pages to the static page /50x.html
2.重啓Nginx服務,訪問本機自動跳轉到 http://192.168.1.100 地址上去.
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
做用:當用戶訪問Nginx負載均衡器時,Nginx將訪問請求平均分配給後端的Apache服務器,實現簡單的負載均衡.
[實驗環境] [IP地址] [主機做用] 192.168.1.100 Nginx負載均衡 192.168.1.10 Apache主機1 192.168.1.20 Apache主機2
1.修改Nginx主配置文件,在相應的區域中加入如下標★語句.
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 17 http { 18 include mime.types; 19 default_type application/octet-stream; 20 sendfile on; 21 keepalive_timeout 65; 22 --------------------------------------------------------------------------------------- ★在下處填寫負載均衡語句 <語句應寫在http語句內而且是在server語句外填寫> ★ upstream lyshark.com { #自定義區域名 ★ ★ server 192.168.1.10:80 weight 1; #負載均衡主機web1 ★ server 192.168.1.20:80 weight 2; #負載均衡主機web2 ★ ★ server 192.168.1.30:80 weight 1 backup; ★ #backup表示機器處於熱備狀態,weight表明權重,權重越高使用越多! ★ } --------------------------------------------------------------------------------------- 31 32 server { 33 listen 80; 34 server_name localhost; 35 --------------------------------------------------------------------------------------- 36 location / { ★ # root html; #註釋掉 ★ # index index.html index.htm; #註釋掉 ★ proxy_pass http://lyshark.com; #代理交給上面的自定義區域處理 40 } --------------------------------------------------------------------------------------- 41 42 error_page 500 502 503 504 /50x.html; 43 location = /50x.html { 44 root html; 45 } 46 } 47 }
2.重啓Nginx服務,此時訪問Nginx時,會自動分配給後端的Apache集羣.
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
做用:證書加密,提升了頁面的安全性.
1.安裝軟件依賴包
[root@localhost ~]# yum install -y pcre-devel zlib-devel openssl openssl-devel
2.創建服務器私鑰,過程當中須要輸入密碼.
[root@localhost ~]# openssl genrsa -des3 -out server.key 1024 ---------------------------------------------------------------------------- [參數解釋] Genrsa –des3 #加密類型 -out server.key #輸出文件 -1024 #加密長度 ----------------------------------------------------------------------------
3.創建證書,生成的csr文件交給CA簽名後造成服務端本身的證書.
[root@localhost ~]# openssl req -new -key server.key -out server.csr ---------------------------------------------------------------------------- [參數解釋] req -new #新建證書 -key server.key #私鑰文件 -out server.csr #輸出文件 #注:依次輸入:國家 省 市 組織 機構 全稱 EMAIL 是否要改變密碼 是否更名稱 ----------------------------------------------------------------------------
4.轉化成證書,這一步由證書CA機構來作的,這裏只是實驗.
[root@localhost ~]# openssl x509 -req -days 365 -sha256 -in server.csr -signkey server.key -out servernew.crt
5.複製證書和密鑰到Nginx目錄下.
[root@localhost ~]# cp -a server.key /usr/local/nginx/conf/server.key #複製密鑰到conf目錄下 [root@localhost ~]# cp -a servernew.crt /usr/local/nginx/conf/server.crt #複製證書到conf目錄下
6.修改Nginx主配置文件,在相應的區域中加入如下標★語句.
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 33 #gzip on; 34 35 server { ★ listen 443; #修改端口爲443 37 server_name localhost; 38 ---------------------------------------------------------------------------- #添加如下內容,啓用證書 ★ ssl on; #開啓SSL加密 ★ ssl_certificate server.crt; #證書位置 ★ ssl_certificate_key server.key; #密鑰位置 ★ ssl_session_timeout 5m; #會話操做時間 ★ ssl_protocols TLSv1; #協議版本 ★ ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM; #指定使用加密算法 ★ ssl_prefer_server_ciphers on; #緩存開啓 ---------------------------------------------------------------------------- 46 47 location / { 48 root html; 49 index index.html index.htm; 50 } 51 52 #error_page 404 /404.html; 53 54 # redirect server error pages to the static page /50x.html
7.重啓Nginx服務,便可實現https加密
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
8.訪問地址,須要在末尾增長:443
[root@localhost ~]# elinks https://127.0.0.1:443
做用:實現訪問跳轉,好比活動頁面暫時跳轉.
[實驗效果] 當用戶訪問: http://127.0.0.1/index.html 將地址跳轉到: http://59.110.167.239/index.html
1.修改Nginx主配置文件,在相應的區域中加入如下標★語句.
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 24 server { 25 listen 80; 26 server_name localhost; 27 28 #charset koi8-r; 29 30 #access_log logs/host.access.log main; 31 32 location / { ---------------------------------------------------------------------------- ★ #root html; #註釋掉 ★ #index index.html index.htm; #註釋掉 ---------------------------------------------------------------------------- 35 ---------------------------------------------------------------------------- #添加如下內容 ★ rewrite ^(.*)$ http://59.110.167.239 permanent; #實現地址全跳轉(訪問本機跳轉到59.110.167.239上) ★ rewrite ^(.*)$ https://$host$1 permanent; #實現自身http到https的全跳轉 ---------------------------------------------------------------------------- 37 } 38 39 #error_page 404 /404.html; 40 41 # redirect server error pages to the static page /50x.html
2.重啓Nginx服務,加載配置
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
3.手動訪問本機 http://127.0.0.1 將自動跳轉到 http://59.110.167.239
[root@localhost ~]# elinks http://127.0.0.1/index.html
做用:當用戶訪問自身http地址時,會自動的跳轉到https地址去訪問.
[實驗效果] 用戶訪問: http://127.0.0.1/index.html 會跳轉到: https://127.0.0.1/index.html
1.安裝軟件依賴包.
[root@localhost ~]# yum install -y pcre-devel zlib-devel openssl openssl-devel
2.創建服務器私鑰,過程當中須要輸入密碼.
[root@localhost ~]# openssl genrsa -des3 -out server.key 1024
3.創建證書,生成的csr文件交給CA簽名後造成服務端本身的證書.
[root@localhost ~]# openssl req -new -key server.key -out server.csr
4.轉化成證書,這一步由證書CA機構來作的,這裏只是實驗.
[root@localhost ~]# openssl x509 -req -days 365 -sha256 -in server.csr -signkey server.key -out servernew.crt
5.複製證書和密鑰到Nginx目錄下
[root@localhost ~]# cp -a server.key /usr/local/nginx/conf/server.key #複製密鑰到conf目錄下 [root@localhost ~]# cp -a servernew.crt /usr/local/nginx/conf/server.crt #複製證書到conf目錄下
6.修改Nginx主配置文件,在相應的區域中加入如下標★語句.
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 17 http { 18 include mime.types; 19 default_type application/octet-stream; 20 sendfile on; 21 keepalive_timeout 65; 22 23 server { 24 listen 80; 25 server_name localhost; 26 27 location / { ---------------------------------------------------------------------------- #修改如下內容 ★ #root html; #註釋掉 ★ #index index.html index.htm; #註釋掉 ★ rewrite ^(.*)$ https://$host$1 permanent; #實現自身http到https的跳轉 ---------------------------------------------------------------------------- 31 } 32 33 error_page 500 502 503 504 /50x.html; 34 location = /50x.html { 35 root html; 36 } 37 } 38 39 server { ★ listen 443; 41 server_name localhost; 42 ---------------------------------------------------------------------------- #添加如下內容,啓用證書 ★ ssl on; #開啓SSL加密 ★ ssl_certificate server.crt; #證書位置 ★ ssl_certificate_key server.key; #密鑰位置 ★ ssl_session_timeout 5m; #會話操做時間 ★ ssl_protocols TLSv1; #協議版本 ★ ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM; #指定使用加密算法 ★ ssl_prefer_server_ciphers on; #緩存開啓 ---------------------------------------------------------------------------- 50 51 location / { 52 root html; 53 index index.html index.htm; 54 55 } 56 57 error_page 500 502 503 504 /50x.html; 58 location = /50x.html { 59 root html; 60 } 61 } 62 }
7.重啓Nginx服務,加載配置.
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
8.手動訪問本機 http://127.0.0.1 將自動跳轉到 https://127.0.0.1.
[root@localhost ~]# elinks http://127.0.0.1
做用:配置HSTS是爲了提升網頁的安全性,以及防止盜連接,瞭解便可.
1.在Https的Server站點添加以下頭部
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
2.在Http的Server站點添加相應代碼
return 301 https://$host;
3.添加X-Frame-Options頭部,確保不會嵌入到Frame 或 iframe 避免點擊劫持
add_header X-Frame-Options "DENY";
4.訪問頁面,測試
chrome://net-internals/
做用:本實戰項目實現了企業環境的負載均衡,並啓動了https認證,看成生產環境無壓力.
[實驗環境] [主機IP] [主機名稱] [主機做用] 192.168.1.12 Nginx 反向代理+https認證 192.168.1.13 Web1 負載主機1 192.168.1.14 Web2 負載主機2 [實驗過程] 1.生成SSL證書 2.配置一個DNS,實現本地解析,將192.168.1.12解析成 www.lyshark.com 3.配置好兩臺後臺Apache服務器,12-13 4.安裝並配置Nginx 5.Nginx能正常訪問後 6.在作作http到https的跳轉 7.緊接着跳轉代理,作負載均衡 8.最後訪問www.lyshark.com實現https跳轉,和壓力分攤
1.配置DNS服務器,DNS服務器解析到本機Nginx服務器上
Nginx 192.168.1.12 解析成 www.lyshark.com
2.配置兩臺Apache服務器
Apache Web1 192.168.1.13 Apache Web2 192.168.1.14
3.安裝並配置Nginx
a)生成證書與私鑰
[root@localhost ~]# openssl genrsa -des3 -out server.key 1024 [root@localhost ~]# openssl req -new -key server.key -out server.csr [root@localhost ~]# openssl x509 -req -days 365 -sha256 -in server.csr -signkey server.key -out servernew.crt
b)複製證書和密鑰到Nginx目錄下
[root@localhost ~]# cp -a server.key /usr/local/nginx/conf/server.key [root@localhost ~]# cp -a servernew.crt /usr/local/nginx/conf/server.crt
4.修改Nginx主配置文件,在相應的區域中加入如下標★語句
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 1 worker_processes 1; 2 3 events { 4 worker_connections 1024; 5 } 6 7 8 http { 9 include mime.types; 10 default_type application/octet-stream; 11 sendfile on; 12 keepalive_timeout 65; 13 ---------------------------------------------------------------------------- #在此處填寫負載均衡語句(在http語句內,server語句外填寫) ★ upstream lyshark.com { #自定義區域名 ★ ★ server 192.168.1.13:80; #均衡主機1 ★ server 192.168.1.14:80; #均衡主機2 ★ } ---------------------------------------------------------------------------- 19 20 21 server { 22 listen 80; ★ server_name www.lyshark.com; #本機域名 24 25 location / { ---------------------------------------------------------------------------- #配置http到https的自身跳轉 ★ #root html; #註釋掉 ★ #index index.html index.htm; #註釋掉 ★ rewrite ^(.*)$ https://$host$1 permanent; #將http請求跳轉到https ---------------------------------------------------------------------------- 29 } 30 31 error_page 500 502 503 504 /50x.html; 32 location = /50x.html { 33 root html; 34 } 35 } 36 37 server { ★ listen 443; #修改端口 ★ server_name www.lyshark.com; #本機域名 40 ---------------------------------------------------------------------------- #添加如下內容,啓用證書 ★ ssl on; #開啓SSL加密 ★ ssl_certificate server.crt; #證書位置 ★ ssl_certificate_key server.key; #密鑰位置 ★ ssl_session_timeout 5m; #會話操做時間 ★ ssl_protocols TLSv1; #協議版本 ★ ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM; #指定使用加密算法 ★ ssl_prefer_server_ciphers on; #緩存開啓 ---------------------------------------------------------------------------- 48 49 location / { ---------------------------------------------------------------------------- #地址跳轉配置 ★ #root html; #註釋掉 ★ #index index.html index.htm; #註釋掉 ★ proxy_pass http://lyshark.com; #實現地址跳轉,有80的請求轉443 ---------------------------------------------------------------------------- 53 } 54 55 error_page 500 502 503 504 /50x.html; 56 location = /50x.html { 57 root html; 58 } 59 } 60 }
5.重啓Nginx服務,加載配置
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)
6.手動訪問 http://127.0.0.1/index.html 將跳轉到 https://127.0.0.1/index.html
[root@localhost ~]# elinks http://127.0.0.1/index.html
開啓nginx監控模塊
寫入 (約在47行) location /lyshark { stub_status on; } #查看監控頁面輸入: http://127.0.0.1/lyshark
源碼編譯修改版本號
vim nginx-1.13.12/src/core/nginx.h #define nginx_version 1013012 #define NGINX_VERSION "1.13.12" #define NGINX_VER "nginx/" NGINX_VERSION //修改完保存退出,編譯便可