1. 程式人生 > >nginx之三:nginx安裝配置+清緩存模塊安裝

nginx之三:nginx安裝配置+清緩存模塊安裝

location clu dev serve 下載軟件 entos 圖片 nts 錯誤頁面

經過一段時間的使用,發現 nginx 在並發與負載能力方面確實優於 apache,現在已經將大部分站點從 apache 轉到 了 nginx 了。以下是 nginx 的一些簡單的安裝配置。

環境

操作系統:CentOS、RedHat

IP 地址:192.168.1.202

下載軟件包

# mkdir /usr/local/src/tarbag
# mkdir /usr/local/src/software
# cd /usr/local/src/tarbag/
# wget http://www.nginx.org/download/nginx-1.0.6.tar.gz
# wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz

編譯安裝

# cd /usr/local/src/tarbag/
# tar -xzvf nginx-1.0.6.tar.gz -C /usr/local/src/software
# tar -xzvf ngx_cache_purge-1.3.tar.gz -C /usr/local/src/software

# cd /usr/local/src/software/nginx-1.0.6
# ./configure \
--prefix=/usr/local/nginx-1.0.6 \ # 安裝路徑

--with-http_stub_status_module \ # 啟用 nginx 狀態模塊

--with-http_ssl_module \ # 啟用 SSL 模塊
--with-http_realip_module \ # 啟用 realip 模塊(將用戶 IP 轉發給後端服務器)

--add-module=../ngx_cache_purge-1.3 # 添加緩存清除擴展模塊

#make

#make install

內核優化

#vim /etc/sysctl.conf

net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 1800

net.ipv4.ip_conntrack_max = 16777216 # 如果使用默認參數,容易出現網絡丟包

net.ipv4.netfilter.ip_conntrack_max = 16777216# 如果使用默認參數,容易出現網絡丟包

net.ipv4.tcp_max_syn_backlog = 65536

net.core.netdev_max_backlog = 32768

net.core.somaxconn = 32768

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 2

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_max_orphans = 3276800

配置生效

#sysctl -p

配置web頁面

#echo 1 > /www/html/www.heytool.com

#echo 2 > /www/html/bbs.heytool.com

修改nginx配置文件

vim /etc/nginx.conf

user nobody nobody; # 運行 nginx 的所屬組和所有者

worker_processes 2; # 開啟兩個 nginx 工作進程,一般幾個 CPU 核心就寫幾error_log logs/error.log notice; # 錯誤日誌路徑
pid logs/nginx.pid; # pid 路徑
events {

worker_connections 1024; # 一個進程能同時處理 1024 個請求

}

http {
  include    mime.types;
  default_type    application/octet-stream;

log_format main

access_log logs/access.log main; # 默認訪問日誌路徑sendfile on;

keepalive_timeout 65; # keepalive 超時時間

# 開始配置一個域名,一個 server 配置段一般對應一個域名

server {

listen 80; #
# 在本機所有 ip 上監聽 80,也可以寫為 192.168.1.202:80,這樣的話,就只監聽 192.168.1.202 上的 80 端口

server_name www.heytool.com; # 域名
root /www/html/www.heytool.com; # 站點根目錄(程序目錄)
index index.html index.htm; # 索引文件

 location / {    #可以有多個localtion
      root    /www/html/www.heytool.com;
}
error_page 500 502 503 504 /50x.html

# 定義錯誤頁面,如果是 500 錯誤,則把站點根目錄下的 50x.html 返回給用戶

location = /50x.html {

root /www/html/www.heytool.com;

}

}

#開始配置站點bbd.heytool.com

server {

listen 80;

server_name bbs.heytool.com;

root /www/html/bbs.heytool.com;

index index.html index.html; #索引文件

location / {

root /www/html/bbs.heytool.com;

error_page 500 502 503 504 /50x.html

location = /50x.html {

root /www/html/bbs.heytool.com;

}

}

}

nginx啟動關閉

#/usr/local/nginx-1.0.6/sbin/nginx //啟動nginx

#/usr/local/nginx-1.0.6/sbin/nginx -t //檢測nginx配置文件

#/usr/local/nginx-1.0.6/sbin/nginx -s reload //重載nginx

#/usr/local/nginx-1.0.6/sbin/nginx -s stop //關閉nginx

測試

創建測試站點

# mkdir –p /www/html/www.heytool.com
# mkdir –p /www/html/bbs.heytool.com
# echo “www.heytool.com” > /www/html/www.heytool.com/index.html

# echo “bbs.heytool.com” > /www/html/bbs.heytool.com/index.html

啟動nginx

# /usr/local/nginx-1.0.6/sbin/nginx –t //看到 ok 和 successful,說明配置文件沒問題nginx: the configuration file

/usr/local/ nginx-1.0.6/conf/nginx.conf syntax is ok nginx: configuration file

/usr/local/ nginx-1.0.6/conf/nginx.conf test is successful

# /usr/local/nginx-1.0.6/sbin/nginx

綁定hosts,測試

把兩個域名指向 192.168.1.202

192.168.1.202 www.heytool.com

192.168.1.202 bbs.heytool.com

打開 www.heytool.com,如下圖:

技術分享圖片

打開 bbs.heytool.com,如下圖:

技術分享圖片 完畢!!!

nginx之三:nginx安裝配置+清緩存模塊安裝