Nginx虛擬主機的搭建的實現(xiàn)步驟
Nginx服務基礎
關于Nginx
一款高性能、輕量級Web服務軟件
穩(wěn)定性高
系統(tǒng)資源消耗低
對HTTP并發(fā)連接的處理能力高
單臺物理服務器可支持30 000~50000個并發(fā)請求
Nginx相對于Apache的優(yōu)點:
輕星級,同樣是web服務,比Apache 占用更少的內(nèi)存及資源﹔高并發(fā),Nginx處理請求是異步非塞的,而Apache
則是阻塞型的,在高并發(fā)下Nginx能保持低資源低消耗高性能;高度模塊化的設計
編寫模塊相對簡單;社區(qū)活躍,各種高性能模塊出品迅速。
Apache相對于Nginx的優(yōu)點:
rewrite,比Nginx的rewrite強大;模塊超多,基本想到的都可以找到;少bug,Nginx的bug相對較多;超穩(wěn)定
存在就是理由,一般來說,需要性能的web 服務,用Nginx。如果不需要性能只求穩(wěn)定,那就Apache。Nginx處理動態(tài)請求是弱項,一般動態(tài)請求要Apache去做,Nginx只適處理靜態(tài)網(wǎng)頁或反向代理。
- Nginx是一個基于事件的Web服務器,Apache是一個基于流程的服務器;
- Nginx所有請求都由一個線程處理,Apache單個線程處理單個請求;
- Nginx避免子進程的概念,Apache是基于子進程的;
- Nginx在內(nèi)存消耗和連接方面更好,Apache在內(nèi)存消耗和連接方面一般;
- Nginx的性能和可伸縮性不依賴于硬件,Apache依賴于CPU和內(nèi)存等硬件;
- Nginx支持熱部署,Apache不支持熱部署;
- Nginx對于靜態(tài)文件處理具有更高效率,Apache相對一般;
- Nginx在反向代理場景具有明顯優(yōu)勢,Apache相對一般。
Nginx訪問控制
安裝
關閉防火墻
安裝依賴包
#nginx的配置及運行需要pcre、zlib等軟件包的支持,因此需要安裝這些軟件的開發(fā)包,以便提供相應的庫和頭文件yum -y install pcre-devel zlib-devel gcc gcc-c++ make#nginx的配置及運行需要pcre、zlib等軟件包的支持,因此需要安裝這些軟件的開發(fā)包,以便提供相應的庫和頭文件 yum -y install pcre-devel zlib-devel gcc gcc-c++ make

創(chuàng)建運行用戶、組
創(chuàng)建運行用戶、組(Nginx服務程序默認以nobody 身份運行,建議為其創(chuàng)建專門的用戶賬號,以便更準確地控制其訪問權限)
useradd -M -s /sbin/nologin nginx
編譯安裝Nginx
cd /opt tar zxvf nginx-1.12.0.tar.gz -C /opt/ cd nginx-1.12.0/ ./ configure \ --prefix=/usr/local/nginx \ #指定nginx的安裝路徑 --user=nginx \ #指定用戶名 --group=nginx \ #指定組名 --with-http_stub_status_module #啟用http_stub_status_module 模塊以支持狀態(tài)統(tǒng)計 make && make install ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #讓系統(tǒng)識別nginx的操作命令

啟動前先關掉之前裝的apache服務

檢查、啟動、重啟、停止 nginx服務
nginx -t #檢查配置文件是否配置正確 nginx #啟動 #停止 cat /usr/local/nginx/logs/nginx.pid #先查看nginx的PID號 kill -3 <PID號> #-3保證數(shù)據(jù)不丟失 kill -s QUIT <PID號> killall -3 nginx killall -s QUIT nginx #重載 kill -1 <PID號> kill -s HUP <PID號> killall -1 nginx killall -s HUP nginx kill -USR1 <PID號> #日志分割,重新打開日志文件 kill -USR2<PID號> #平滑升級 nginx -v #查看版本號 新版本升級: tar -zxvf nginx-1.xx.xx.tar.gz cd nginx-1.xx.xx ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module \ --with-http_ssl_module

nginx #啟動服務
檢查端口是否開啟

查看進程號

使用kill命令殺死進程號即可停止nginx服務
配置 Nginx服務管理
vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecrReload=/bin/kill -s HUP $MAINPID ExecrStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target chmod 754 /lib/systemd/system/nginx.service systemctl start nginx.service systemctl enable nginx.service

認識Nginx服務的主配值文件nginx.conf
1.全局塊:
配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日志存放路徑,配置文件引入,允許生成worker process數(shù)等。
2.events塊:
配置影響nginx服務器或與用戶的網(wǎng)絡連接。有每個進程的最大連接數(shù),選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網(wǎng)路連接,開啟多個網(wǎng)絡連接序列化等。
3.http塊:
可以嵌套多個server,配置代理,緩存,日志定義等絕大多數(shù)功能和第三方模塊的配置。
如文件引入,mime-type定義,日志自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數(shù)等。
4.server塊:
配置虛擬主機的相關參數(shù),一個http中可以有多個server。
5.location塊:
配置請求的路由,以及各種頁面的處理情況。
vim /usr/local/nginx/conf/nginx.conf
1.全局設置
cat /proc/ cpuinfo l grep -c processor#查看cpu內(nèi)核數(shù) #user nobody; #運行用戶,若編譯時未指定則默認為nobody worker_processes 1; #工作進程數(shù)量,可配置成服務器內(nèi)核數(shù)*2,如果網(wǎng)站訪問量不大,一般設為1就夠用了 #error_log logs/error.log; #錯誤日志文件的位置 #pid logs/nginx.pid; #PID文件的位置
2.I/o事件配置
events {
use epoll; #使用epoll模型,2.6及以上版本的系統(tǒng)內(nèi)核,建議使用epoll模型以提高性能
worker_connections 4096; #每個進程處理4096個連接
}
#如提高每個進程的連接數(shù)還需執(zhí)行"ulimit -n 65535"命令臨時修改本地每個進程可以同時打開的最大文件數(shù)。
#在Linux平臺上,在進行高并發(fā)TCP連接處理時,最高的并發(fā)數(shù)量都要受到系統(tǒng)對用戶單一進程同時可打開文件數(shù)量的限制(這是因為系統(tǒng)為每個TCP連接都要創(chuàng)建一個socket句柄,每個socket句柄同時也是一個文件句柄)。
#可使用ulimit -a命令查看系統(tǒng)允許當前用戶進程打開的文件數(shù)限制。
#epoll是Linux內(nèi)核為處理大批句柄而作改進的poll,是Linux下多路復用IO接口select/poll的增強版本,它能顯著的減少程序在大量并發(fā)連接中只有少量活躍的情況下的系統(tǒng)CPU利用率。
uname -r #查看內(nèi)核版本
3.HTTP配置
http {
##文件擴展名與文件類型映射表
include mime.types;
##默認文件類型
default_typeapplication/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"';
##訪問日志位置
#access_log logs/access.log main;
##支持文件發(fā)送(下載)
sendfile on;
##此選項允許或禁止使用socket的TCP_CORK的選項(發(fā)送數(shù)據(jù)包前先緩存數(shù)據(jù)),此選項僅在使用sendfile的時候使用
#tcp_nopush on;
##連接保持超時時間,單位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
##gzip模塊設置,設置是否開啟gzip壓縮輸出
#gzip on;
##web 服務的監(jiān)聽配置
server {
##監(jiān)聽地址及端口
listen 80;
##站點域名,可以有多個,用空格隔開
server_name www.jzm.com;
##網(wǎng)頁的默認字符集
charset utf-8;
##根目錄配置
location / {
##網(wǎng)站根目錄的位置/usr/local/nginx/html
root html;
##默認首頁文件名
index index.html index.php;
}
##內(nèi)部錯誤的反饋頁面
error_page 500 502 503 504/50x.html;
##錯誤頁面配置
location = /50x.html {
root html;
}
}
}
日志格式設定
$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址; $remote_user:用來記錄客戶端用戶名稱; $time_local:用來記錄訪問時間與時區(qū); $request:用來記錄請求的url與http協(xié)議; $status:用來記錄請求狀態(tài)(成功是200); $body_bytes_sent:記錄發(fā)送給客戶端文件主體內(nèi)容大??; $http_referer:用來記錄從哪個頁面鏈接訪問過來的; $http_user_agent:記錄客戶瀏覽器的相關信息; 通常web服務器放在反向代理的后面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務器的iP地址。 反向代理服務器在轉發(fā)請求的http頭信息中,可以增加x_forwarded_for信息,以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址。 location常見配置指令,root、alias、proxy_pass root(根路徑配置):root /var/www/html 請求www.jzm.com/test/1.html,會返回文件/var/www/html/test/1.html
訪問狀態(tài)統(tǒng)計配置
1.先使用命令查看已安裝的Nginx是否包含HTTP_STUB_STATUS模塊
/usr/local/nginx/sbin/nginx -V cat /opt/nginx-1.12.0/auto/options | grep YES #可查看nginx已安裝的所有模塊
修改nginx.conf 配置文件,指定訪問位置并添加 stub_status 配置
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/ local/nginx/ conf/ nginx.conf
......
http {
......
server {
listen 80;
server_name www.jzm.com;
charset utf-8;
location / {
root html;
index index.html index.php;
}
#添加stub_status配置
location /status { #訪問位置為/status
stub_status on; #打開狀態(tài)統(tǒng)計功能
access_log off; #關閉此位置的日志記錄
}
}
}
重啟服務,訪問測試
systemctl restart nginx 瀏覽器訪問http://192.168.80.30/status Active connections:表示當前的活動連接數(shù); server accepts handled requests:表示已經(jīng)處理的連接信息,三個數(shù)字依次表示已處理的連接數(shù)、成功的TCP握手次數(shù)、己處理的請求數(shù)。可curl -Ls http://192.168.80.20/status 結合awk與if語句進行性能監(jiān)控。
并發(fā)量檢測腳本
#!/bin/bash
#設置變量,獲取當前活躍的連接數(shù)
num=$(curl -Ls http://192.168.80.30/status | awk '/Active connections/ {print $3}')
if [ "$num" -gt 2 ];then
echo "警告!當前web服務并發(fā)量過大!當前并發(fā)量為 $num"
fi
基于授權的訪問控制
生成用戶登錄碼認證文件
yum install -y httpd-tools htpasswd -c /usr/local/nginx/userlist zhangsan #第一次一定要加-c cat /usr/local/nginx/userlist chown nginx /usr/local/nginx/userlist chmod 400 /usr/local/nginx/userlist

修改主配置文件相對應目錄,添加認證配置項
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
#添加認證配置
auth_basic "secret";
#設置登錄碼提示框文字信息
auth_basic_user_file /usr/local/nginx/passwd.db;
}
}

重啟服務,訪問測試
登錄后復制
nginx -t systemctl restart nginx
瀏覽器訪問http://192.168.80.20

基于客戶端的訪問控制
訪問控制規(guī)則如下
deny IP/IP段:拒絕某個IP或IP段的客戶端訪問。allow IP/IP 段:允許某個IP或IP段的客戶端訪問。規(guī)則從上往下執(zhí)行,如匹配則停止,不再往下匹配。
vim /usr/ local/nginx/conf/nginx.conf
......
server {
location /{
......
##添加控制規(guī)則##
allow 192.168.80.200;
#允許訪問的客戶端IP
deny all;
#拒絕其它IP客戶端訪問
systemctl restart nginx
基于域名的Nginx虛擬主機
提供域名解析
echo "192.168.80.20 www.pll.com www.benet.com" >> /etc/hosts
為虛擬主機添加測試文件
mkdir -p /var/www/html/benet mkdir -p /var/www/html/kgc echo "www.pll.com" > /var/www/html/jzm/index.html echo "www.benet.com" > /var/www /html/benet/index.html
修改Nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.jzm.com;
charset utf-8;
access_log logs/www.jzm.access.log;
location / {
root /var/www/html/jzm;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
#檢查語法
nginx -t
systemctl restart nginx
#瀏覽器訪問
http://www.jzm.com/
http://www.benet.com/
基于IP的Nginx虛擬主機
ifconfig ens33:0 192.168.80.120 netmask 255.255.255.0 echo "192.168.80.20 www.jzm.com" >> /etc/hosts echo "192.168.80.120 www.benet.com" >> /etc/hosts
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.80.20:80;
server_name www.jzm.com;
charset utf-8;
access_log logs/www.jzm.access.log;
location / {
root /var/www/html/jzm;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.80.120:80;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
systemctl restart nginx
#瀏覽器訪問
http://192.168.80.20/
http://192.168.80.120/
基于端口的 Nginx 虛擬主機
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.80.20:80;
server_name www.jzm.com;
charset utf-8;
access_log logs/www.jzm.access.log;
location / {
root /var/www/html/jzm;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.80.120:8080;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
systemctl restart nginx
瀏覽器訪問
http://192.168.80.20:8080
http://192.168.80.120:8888
到此這篇關于Nginx虛擬主機的搭建的實現(xiàn)步驟的文章就介紹到這了,更多相關Nginx虛擬主機的搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Nginx實戰(zhàn)之讓用戶通過用戶名密碼認證訪問web站點
這篇文章主要介紹了詳解Nginx實戰(zhàn)之讓用戶通過用戶名密碼認證訪問web站點,有興趣的可以了解一下。2016-11-11
Windows下使用Nginx+Tomcat做負載均衡的完整步驟
這篇文章主要介紹了Windows下使用Nginx+Tomcat做負載均衡的完整步驟,幫助大家搭建負載均衡集群,感興趣的朋友可以了解下2020-09-09
nginx rewrite重寫規(guī)則與防盜鏈配置方法教程詳解
這篇文章主要介紹了nginx rewrite重寫規(guī)則與防盜鏈配置方法教程詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Nginx漏洞整改實現(xiàn)限制IP訪問&隱藏nginx版本信息
本文主要介紹了Nginx漏洞整改實現(xiàn)限制IP訪問&隱藏nginx版本信息,通過配置Nginx的ACL,可以輕松實現(xiàn),下面就來具體介紹一下,感興趣的可以了解一下2024-03-03

