整體流程:1.搭建tomcat項目集群(默認完成) 2.安裝nginx需要的庫 3.安裝Nginx並修改配置文件 4.啟動測試
1.1.1. 安裝Nginx
1.1.1.1. 安裝環境:
安裝pcre庫
yum -y install pcre-devel
安裝zlib庫
yum install -y zlib-devel
安裝openssl庫
yum install -y openssl openssl-devel 或者 編譯安裝
編譯安裝openssl:
1.上傳openssl壓縮包
按alt+p進入上傳界面,上傳openssl-1.0.1t.tar.gz
2.解壓壓縮包
[root@-01 ~]# tar –zxvf openssl-1.0.1t.tar.gz
[root@-01 ~]#cd openssl-1.0.1t
3.編譯安裝
設置安裝參數
[root@-01 openssl-1.0.1t]# ./config
編譯並安裝
[root@-01 nginx-1.7.7]#make
[root@-01 nginx-1.7.7]#make install
准備安裝Nginx:
上傳Nginx
按alt+p進入上傳界面,上傳Nginx
1.1.1.2. 解壓
解壓
[root@-01 ~]# tar -zxvf nginx-1.10.2.tar.gz
進入解壓文件夾
[root@-01 ~]# cd nginx-1.10.2
1.1.1.3. 編譯安裝
設置安裝參數
[root@-01 nginx-1.10.2]#./configure --prefix=/usr/local/nginx --with-http_ssl_module(這個是采用https方式訪問需要安裝的模塊,用http不需要安裝)
編譯並安裝
[root@itcast-01 nginx-1.10.2]# make
編譯成功效果
[root@itcast-01 nginx-1.10.2]# make install
openssl生成測試CA證書:
# 1、首先,進入你想創建證書和私鑰的目錄,例如: cd /usr/local/nginx/ # 2、創建服務器私鑰,命令會讓你輸入一個口令: openssl genrsa -des3 -out server.key 2048
[lenin@archer ~]$ openssl genrsa -des3 -out root.key
Generating RSA private key, 512 bit long modulus
……………..++++++++++++
..++++++++++++
e is 65537 (0×10001)
Enter pass phrase for root.key: ← 輸入一個新密碼
Verifying – Enter pass phrase for root.key: ← 重新輸入一遍密碼
# 3、創建簽名請求的證書(CSR):
openssl req -new -key server.key -out server.csr
Enter pass phrase for root.key: ← 輸入前面創建的密碼
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:CN ← 國家代號,中國輸入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不輸入
Common Name (eg, YOUR name) []: ← 此時不輸入
Email Address []:admin@mycompany.com ← 電子郵箱,可隨意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不輸入
An optional company name []: ← 可以不輸入
# 4、在加載SSL支持的Nginx並使用上述私鑰時除去必須的口令:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
# 5、最后標記證書使用上述私鑰和CSR:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
1.1.1.4. 修改nginx.conf文件,實現負載均衡:
需求:1.用戶通過https訪問
2.實現頁面壓縮gzip
3.記錄用戶真實ip地址
4.使用ip-hash方式創建集群信息,解決session粘滯問題
5.nginx管理靜態資源
配置文件如下:(以下參數詳細介紹參考http://www.cnblogs.com/zclzhao/p/5033391.html)
#user nobody; worker_processes 2;#根據自己系統cpu數定,一般等於核心數或者核心數的兩倍 user root;#開啟root用戶權限,有時候訪問盤符下資源會報403沒有權限,開啟root可以解決 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
#pid logs/nginx.pid;
worker_rlimit_nofile 65535; events { worker_connections 65535;#連接數 use epoll;#牛逼模式 }
http { 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"'; #配置集群信息 upstream xxx { ip_hash; server xxx.xx.xxx.xx:8083; server xxx.xx.xxx.xx:8085; }
#access_log logs/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 120;
server_tokens off; charset utf-8; #開啟壓縮 gzip on; gzip_min_length 1k; # fastcgi_connect_timeout 300; 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 www.xxx.com;
ssl_certificate /usr/local/nginx/server.crt; ssl_certificate_key /usr/local/nginx/server.key;
ssl_session_cache shared:SSL:1m; ssl_session_timeout 5000m;
# ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location ~ .*\.(ico|png|jpg|eot|svg|ttf|woff|js|css)$ location / { #配置用戶真實ip,部分虛擬機配置真實ip后報404,可注掉. #proxy_set_header Host $host; # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #使用之前配置的集群 proxy_pass http://xxx; } }
} |
注意:1.location塊root的路徑問題:例如:location塊配置如下
location ~ \.png$
{
root /home;
}
項目圖片訪問路徑為/images/a.png,那么匹配正則后路徑變為/home/images/a.png.
圖片地址src除去被監聽的域名后能和root后邊的路徑組成被訪問的路徑。
以上配置文件location正則uri僅為示例。
2.進入location塊root路徑的前提是nginx必須要先監聽到,進入相應的server塊后,所以圖片路徑一定要帶上要監聽的域名。
1.1.1.5. 啟動Nginx
查看安裝文件,conf是配置文件,sbin是啟動目錄
[root@-01 nginx-1.10.2]# cd /usr/local/nginx/
進入啟動文件目錄,啟動Nginx
[root@itcast-01 nginx]# cd sbin/
[root@itcast-01 sbin]# ./nginx
查看啟動進程
關閉防火牆
[root@itcast-01 sbin]# service iptables stop
訪問測試
1.1.1.6. Nginx相關擴充:
訪問流程:在不添加‘可選匹配規則’模塊時,Nginx服務器首先在server塊的多個location塊中搜索是否有標准uri和請求字符串匹配,如果有多個可以匹配,就記錄匹配度最高的一個。然后,服務器再用location塊中的正則uri和請求字符串匹配,當地一個正則uri匹配成功,就不再進行搜索,並使用這個location塊處理此請求,如果正則匹配全部失敗,就使用剛才記錄的匹配度最高的location塊處理此請求。
查看liunx最大連接數等狀態
[root@itcast-01]# ulimit –a
修改最大進程數
[root@itcast-01]# ulimit -u 65535
修改每個進程可打開的文件數,缺省值是 1024。
[root@itcast-01]# ulimit -n 65535
命令:
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
./nginx -s quit:此方式停止步驟是待nginx進程處理任務完畢進行停止。
./nginx -s stop:此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。
查詢nginx進程:
ps aux|grep nginx
8.2.6 重啟服務:
1.先停止再啟動(推薦):
對 nginx 進行重啟相當於先停止再啟動,即先執行停止命令再執行啟動命令。如下:
./nginx -s quit
./nginx
2.重新加載配置文件:
當 ngin x的配置文件 nginx.conf 修改后,要想讓配置生效需要重啟 nginx,使用-s reload不用先停止 nginx再啟動 nginx 即可將配置信息在 nginx 中生效,如下:
./nginx -s reload
8.2.7 訪問: http://xx.xx.xx.xxx/ 是否成功加載nginx歡迎頁面,如果看到Welcome to nginx!字樣則證明安裝成功
8.2.8 查看nginx 安裝路徑 whereis nginx