1. 程式人生 > >nginx 的基本配置

nginx 的基本配置

nginx的基本配置

nginx的基本配置
1.tar zxf nginx-1.12.0.tar.gz
vim nginx-1.12.0/src/core/nginx.h
12 #define nginx_version 1012000
13 #define NGINX_VERSION "1.12.0"
14 #define NGINX_VER "nginx/" (隱藏nginx的版本號)
vim nginx-1.12.0/auto/cc/gcc
171 # debug
172 #CFLAGS="$CFLAGS -g" 關閉debug模塊,減少nginx模塊大小

2.開始編譯

cd nginx-1.12.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
yum install -y pcre-devel 解決依賴,缺少什麽裝什麽
make
make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 做個軟連接
nginx -t 檢查語法錯誤
nginx 啟動nginx
此時nginx安裝完成

3.nginx 實現反向代理和負載均衡(主服務器server1,後端的負載均衡服務器server2和server3)

server2和server3服務器分別安裝http服務器,並且在默認發布目錄下寫上你需要的東西,然後啟動http服務器
2 user nginx nginx;
12 events {
13 worker_connections 65535;
14 }
upstream westos {
35 server 172.25.45.2:80;
36 server 172.25.45.3:80; http中增加upstream模塊
37 }
server {
122 listen 80;
123 server_name www.westos.org; http中增加反向代理模塊
124
125 location / {
126 proxy_pass http://westos;
127 }
128 }
nginx -t
nginx -s reload
測試
真機:訪問www.westos.org
server2和server3輪詢

4.nginx自帶健康檢查

34 upstream westos {
35 server 172.25.45.2:80;130
36 server 172.25.45.3:80;
37 server 127.0.0.1:8080 backup;
38 }

130 server {
131 listen 8080;
132 server_name 127.0.0.1;
133
134 location / {
135 root /backup;
136 index index.html;
137 charset utf-8; 識別中文 添加一個虛擬主機模塊
138 }
139 }
nginx -t
nginx -s reload
mkdir /backup
echo "系統正在維護中.." > /backup/index.html
測試;當同時關掉server2和server3服務器時,頁面顯示“系統正在維護中”

5.第三方sticky模塊,持續連接,解決session丟失問題

重新編譯安裝一個低版本的nginx軟件包
cd /root
tar zxf nginx-1.10.1.tar.gz
tar zxf nginx-sticky-module-ng.tar.gz
cd nginx-1.10.1
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-sticky-module-ng
make
make install
cp nginx.conf /usr/local/nginx/conf/ 將前面做實驗的conf文件拷貝過來
vim /usr/local/nginx/conf/nginx.conf
34 upstream westos {
35 sticky;
36 server 172.25.45.2:80;
37 server 172.25.45.3:80;
38 # server 127.0.0.1:8080 backup;
39 }
nginx -t
nginx -s reload

測試:
瀏覽器訪問www.westos.org,進入持續連接的狀態,當一臺服務器出現問題時,才會持續連接到另外一臺服務器上

nginx 的基本配置