nginx簡介
Nginx (engine x) 是一個高性能的HTTP和反向代理web服務器,同時也提供了IMAP/POP3/SMTP服務,其將源代碼以類BSD許可證的形式發布,因它的穩定性、豐富的功能集、示例組態檔和低系統資源的消耗而聞名,
百度、京東、新浪、網易、騰訊、淘寶等都在使用nginx服務器,
Nginx的特點:
穩定性極強,7*24小時不間斷運行,
Nginx提供了非常豐富的配置實體,
占用記憶體小,并發能力強
能承受5w并發
nginx官網:http://nginx.org/
軟體包:下載地址
安裝
- yum安裝依賴關系
yum -y install pcre-devel zlib-devel
- 創建用戶
useradd -M -s /sbin/nologin nginx
- 安裝nginx(將下載的原始碼包解包安裝)
tar zxvf nginx-1.12.0.tar.gz -C /usr/src/tar zxvf nginx-1.12.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
--with-http_stub_status_module:安裝nginx統計模塊
-
編譯安裝:
make && make install -
優化路徑
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ -
創建服務腳本
vim /etc/rc.d/init.d/nginx
#!/bin/bash
#chkconfig: 345 85 21
case $1 in
start)
/usr/local/sbin/nginx
;;
stop)
killall -9 nginx
rm -f /var/run/nginx.pid
;;
restart)
$0 stop
$0 start
;;
*)
echo "start|stop|restart"
;;
esac
- 賦予權限,給系統添加服務腳本
chmod +x /etc/rc.d/init.d/nginx
chkconfig --add nginx
重啟服務
關閉:systemctl stop nginx
開啟:systemctl start nginx
此時安裝成功即可訪問默認站點

配置
主組態檔位置:/usr/local/nginx/conf/nginx.conf
引數解釋
user nginx; 默認管理用戶
worker_processes 1; 指定處理器數量,CPU會影響對用戶請求的處理量
pid logs/nginx.pid; 行程ID號管理檔案
events {
worker_connections 1024; //連接數量
use epoll;
}
http { //虛擬主機
include mime.types;
服務支持的檔案型別 路徑:/usr/local/nginx/conf/mime.types
default_type application/octet-stream;
默認MIME型別
sendfile on;
用戶提升硬碟傳輸,如果構建檔案下載類站點,則關閉此選項,用戶平衡網路介面的I/O傳輸
keepalive_timeout 65;
保持會話超時時間,0表示不保持會話
#gzip on; //支持壓縮檔案
server { //web站點配置
listen 80; //監聽埠
server_name localhost; //主機頭部名稱(域名)
charset utf-8; //語言型別
location / { //站點配置
root html;
站點頁面根目錄,默認位置:/usr/local/nginx/html/index.html
index index.html index.htm; //主頁索引檔案
}
}
創建網站
修改組態檔即可,IP埠號均可自己修改

mkdir -p /var/www/baidu
mkdir -p /var/www/sohu
vim /var/www/baidu/index.html 內容:baidu(自定)
vim /var/www/sohu/index.html 內容:sohu(自定)
- 沒有DNS時,編輯虛擬主機檔案
vim /etc/hosts
192.168.1.1 www.baidu.com
192.168.1.1 www.sohu.com
重啟服務訪問驗證
systemctl restart nginx
firefox http://www.baidu.com
firefox http://www.sohu.com
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/246156.html
標籤:其他
