Nginx 是一款高性能的Web服務器軟件.html
具備極高的併發性能nginx
利用Nginx與Tomcat組合使用, 搭建反向代理集羣web
一、安裝服務器
Yum安裝session
安裝併發
yum -y install nginxide
啓動、中止、重啓、開機自啓高併發
systemctl start|stop | restart | enable nginx.service性能
檢查進程測試
ps -aux|grep nginx
源碼編譯安裝
下載源代碼
wget http://nginx.org/download/nginx-1.12.2.tar.gz
建立nginx用戶
useradd nginx
建立nginx安裝目錄
mkdir /usr/local/nginx
安裝編譯時候的依賴包
yum -y install pcre-devel openssl openssl-devel
解壓而且編譯
tar -zxf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/usr/local/nginx --user=nginx --with-http_ssl_module
make
make install
運行nginx
nginx -c /usr/local/nginx/conf/nginx.conf
檢查進程
ps -aux|grep nginx
二、配置
nginx配置文件位置
編譯安裝版本
/usr/local/nginx/conf/nginx.conf
yum安裝版本
/etc/nginx/nginx.conf
nginx配置文件結構
通用(全局)配置參數
http{
http 協議通用參數
server{ 虛擬機配置參數 } server{ 虛擬機配置參數 }
}
三、虛擬主機
在一個Web服務器上經過服務器軟件模擬多臺Web 服務器, 其中每一個虛擬的Web服務器稱爲一個虛擬主機. 虛擬主機的好處是能夠充分複用同一個web服務器. 對於用戶來講, 用戶感受是多個網站.
Nginx 配置文件中 每一個 server{} 塊對應一個虛擬主機
虛擬主機有3種:
基於域名的虛擬主機(最經常使用的虛擬主機)
須要爲服務器指定多個域名
域名資源解析方便, 便於用戶記憶, 用戶體驗好.
基於IP虛擬主機
須要爲服務器指定多個IP地址
須要租用IP
不多使用這種方式
基於端口的虛擬主機
綁定到服務器的多個端口
默認80端口只有一個
使用其餘端口號提供服務, 由於用戶須要記憶端口, 用戶的體驗差
請求 t1.canglaoshi.org 訪問 t1文件夾 index.html文件
server{
listen 80;
server_name t1.canglaoshi.com;
location / {
root t1;
index index.html;
}
}
在Nginx文件夾/usr/local/nginx中添加新文件夾t1和文件index.html
測試:
http://t1.canglaoshi.com
四、https加密訪問
nginx.conf
test.conf
server{
listen 80;
server_name test.canglaoshi.com;
return 301 https://test.canglaoshi.com;
}
server{
listen 443;
server_name test.canglaoshi.com;
ssl on; ssl_certificate cert/xxxxxxxxxxxxx.pem; ssl_certificate_key cert/xxxxxxxxxxxxx.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root test; index index.html; }
}重啓nginx訪問https://test.canglaoshi.com 便可