實例一、Nginx 虛擬主機配置
拷貝一份配置文件,配置虛擬主機;並且創建一個 nginx 的普通用戶
[root@centos-75 ~]# cd /usr/local/nginx/conf
[root@centos-75 conf]# cp nginx.conf.default nginx.conf
[root@centos-75 conf]# useradd nginx -s /sbin/nologin
配置虛擬主機: www.gz1.com 以及配置文件詳解
[root@centos-75 ~]# vim nginx.conf
1)全局變量 部分:-----------------------------------------------------------
user nginx nginx; ##用戶和用戶組都爲 nginx
worker_processes 1; ##Nginx開啓的進程數,建議設置爲等於CPU總核心數
error_log /usr/local/nginx/logs/error.log info; ##定義全局錯誤日誌定義類型
pid /usr/local/nginx/logs/nginx.pid; ##指定進程ID存儲文件位置
2)事件驅動模型配置 部分:-------------------------------------------------------
events {
worker_connections 1024; ##每個進程可以處理的最大連接數
#multi_accept on; ##一定程度降低負載,但服務器吞吐量大時
}
3)設定http 的部分:-----------------------------------------------------------
http {
include mime.types; ##文件擴展名與文件類型映射表,設定mime類型,類型由mime.type文件定義
default_type application/octet-stream; ##默認文件類型
charset utf-8; ##服務器默認編碼
#include /etc/nginx/proxy.conf; ##反向代理配置
#include /etc/nginx/fastcgi.conf; ##fastcgi配置
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; ##訪問日誌
sendfile on; ##是否調用 sendfile 函數來輸出文件(對於普通應用)
tcp_nopush off; ##禁止了小包的發送
tcp_nodelay on; ##允許小包的發送
#keepalive_timeout 0;
keepalive_timeout 10; ##連接超時時間,單位是秒
#gzip模塊設置 : ------------------------------------------------------
#gzip on; ##開啓gzip壓縮輸出
#gzip_min_length 1k; ##最小壓縮文件大小
#gzip_buffers 4 16k; ##壓縮緩衝區
#gzip_http_version 1.0; ##壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)
#gzip_comp_level 2; ##壓縮等級
#gzip_types text/plain application/x-javascript text/css application/xml;
##壓縮類型,默認就已經包含text/html
#設定負載均衡的服務器列表 :(nginx 作爲 web 代理角色 )--------------------
#upstream tomcat { ##tomcat 命名
#server 127.0.0.1:8080 weight=1; ## weight 權值越高被分配到的機率越大
#server 192.168.1.116:8081 weight=1;
#}
#upstream tomcat2 { ##可以配置多個負載均衡的服務器列表
#server 127.0.0.1:8080 weight=1;
#server 192.168.1.116:8081 weight=1;
#}
#虛擬主機的配置 :-------------------------------------------------------
server {
listen 80; ##監聽端口
server_name www.gz1.com,www.gz2.com; ##域名可以有多個,用空格隔開
#charset utf-8; ##字符編碼(上面寫有下面可以不用寫)
access_log /data/web/gz1.com/logs/gz1.access.log main; ##定義本虛擬主機的訪問日誌
#autoindex on; ##所有的文件都以目錄形式訪問
location / {
root /data/web/gz1.com; ##訪問路徑
index index.html index.htm; ##定義索引首頁
proxy_intercept_errors on; ##自定義404界面
}
error_page 404 /404.html; ##在根鬚根下尋找404.html
error_page 500 502 503 504 /50x.html;
location = /404.html { ##錯誤頁面都在此路徑下尋找
root /data/web/gz1.com;
}
}
}
在 客戶端 C:\Windows\System32\drivers\etc 上添加解析
192.168.10.75 www.gz1.com
192.168.10.75 www.gz2.com
在 /data/web/gz1.com 下創建對應的首頁文件以及相應的目錄
[root@centos-75 ~]# mkdir -p /data/web/gz1.com/logs
[root@centos-75 gz1.com]# ls
404.html index.html logs
實例二、Nginx 訪問控制
1、基於用戶的訪問控制:
1)、創建用戶,並且認證加密。此創建的用戶並非 linux 的登陸用戶
[root@centos-75 conf]# yum install -y httpd-tools ##安裝插件
[root@centos-75 conf]# htpasswd -cm /usr/local/nginx/conf/passwd user1
## “c” 表示是第一次創建 “m” 表示MD5加密
New password:
Re-type new password:
Adding password for user user1
[root@centos-75 conf]# htpasswd -m /usr/local/nginx/conf/passwd user2
## 後續創建不需要加 “c”
New password:
Re-type new password:
Adding password for user user2
[root@centos-75 conf]# cat passwd
user1:$apr1$ZBflnq4Y$czYhTd0xG4JoXHvNaoY9B.
user2:$apr1$WD8DoNj2$oQikEyEP3gweTAUKJyb3C0
2)、虛擬主機添加的配置段
[root@centos-75 ~]# vim /usr/local/nginx/conf/nginx.conf
······
location /logs {
root /data/web/gz1.com; ####訪問路徑
auth_basic "nginx "; ##基礎認證 命名nginx(可自定義)
auth_basic_user_file /usr/local/nginx/conf/passwd; ##密碼認證路徑
autoindex on; ##顯示的文件以樹狀陳列
}
······
3)、訪問 www.gz1.com/logs 登陸認證
4)、登陸成功,訪問
2、基於主機的訪問控制:
1)、在 /data/web/gz1.com 下創建一個 test.txt 文件測試
[root@centos-75 gz1.com]# vim test.txt
只允許 192.168.10.1 訪問
拒絕其他ip訪問
2)、虛擬主機添加的配置段
[root@centos-75 ~]# vim /usr/local/nginx/conf/nginx.conf
······
location ~* /.*\.txt$ {
root /data/web/gz1.com;
allow 192.168.10.1;
deny all;
}
······
3)、訪問 www.gz1.com/test.txt
用不是 192.168.10.1 的其他主機訪問;顯示403錯誤,沒有權限訪問
實例三、Nginx 地址重寫 rewrite:
地址重寫
1)、地址從寫訪問 /static 下所有文件都會跳轉到 /write/2.html (創建對應的目錄和文件)
[root@centos-75 gz1.com]# ls
404.html index.html logs static test.txt write
[root@centos-75 gz1.com]# ls write/ static/
static/:
1.html
write/:
2.html
2)、網頁重寫虛擬主機添加的配置段
[root@centos-75 ~]# vim /usr/local/nginx/conf/nginx.conf
······
location /static {
root /data/web/gz1.com;
rewrite .* /write/2.html permanent;
}
······
3)、訪問 http://www.gz1.com/static 會自動跳轉到 http://www.gz1.com/write/2.html
域名重寫,引用 if 判斷
訪問 www.gz2.com 的定向到 www.baidu.com;域名重寫虛擬主機添加的配置段
[root@centos-75 ~]# vim /usr/local/nginx/conf/nginx.conf
······
server {
location / {
root /data/web/gz1.com;
index index.html index.htm;
proxy_intercept_errors on;
if ( $host ~* www.gz2.com ) {
rewrite .* http://www.baidu.com permanent;
}
}
}
······
實例四、Nginx反向代理 :
1、通用匹配 代理
······
location /tset1 {
root /data/web/gz1.com;
proxy_pass http://www.jd.com;
}
······
2、正則表達式 匹配代理
······
location ~ ^/test2 {
root /data/web/gz1.com;
proxy_pass http://www.taobao.com;
}
······
3、地址重寫 代理
······
location / {
root /data/web/gz1.com;
rewrite /(.*)$ /index.php?page=$1 break;
proxy_pass http://www.tmall.com;
}
······
4、proxy模塊的指令
proxy_pass: ##指定將請求代理至upstream server的URL路徑;
proxy_set_header: ##發送至upsream server的報文的某首部進行重寫;
proxy_intercept_errors on; ##攔截後端服務器大於300 的錯誤信息(自定義404頁面)
在根系下配置 proxy_intercept_errors on; 自定義 404 頁面
······
server {
location / {
root /data/web/gz1.com;
index index.html index.htm;
proxy_intercept_errors on;
}
error_page 404 /404.html;
location = /404.html {
root /data/web/gz1.com;
}
}
······
實例五、Gzip 傳輸壓縮功能:
在 http 內(server 虛擬主機配置段外):--------------------------------
······
http{
gzip on; ##開啓Gzip
gzip_min_length 1k; ##大於1K的才壓縮,一般不用改
gzip_buffers 4 16k; ##用於壓縮的緩衝區大小
gzip_comp_level 2; ##壓縮級別,1-10,數字越大壓縮的越好,時間也越長
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php ;
}
······
實例六、負載均衡:
添加 upstream 模塊
······
http{
upstream nginx {
server 192.168.10.74 weight=1 max_fails=10 fail_timeout=10s;
server 192.168.10.75 weight=1 max_fails=10 fail_timeout=10s;
}
}
server {
location / {
proxy_pass http://nginx;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
······
在 客戶端 C:\Windows\System32\drivers\etc 上添加解析
192.168.10.73 www.gz1.com
訪問 www.gz1.com 不斷刷新觀察效果
實例七、Nginx 的緩存模塊功能:
······
http {
proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g;
server {
......
location / {
......
proxy_cache first;
proxy_cache_valid 200 10m;
}
}
}
······
[root@centos-75 ~]# mkdir -p /nginx/cache/first
在 http{ }段(server段前),添加以下語句,在服務器響應報文中,添加一個首部
add_header X-cache "$upstream_cache_status from $server_addr" ;
刪除緩存路徑下的內容,再次觀察。
[root@centos-75 ~]# rm -rf /nginx/cache/first/*
實例八、Nginx 的限速模塊 以及 其他模塊:
1、限速模塊
······
http{
limit_conn_zone $binary_remote_addr zone=one:10m;
server {
location /download {
limit_conn one 1; ##限制在one中記錄狀態的每個IP只能發起一個併發連接
limit_rate_after 1000k; ##在下載1000k後開始限速
limit_rate 100k; ##對每個連接限速100k
}
}
}
······
2、限制單個IP最大連接數(線程數)
http {
limit_conn_zone $binary_remote_addr zone=client_addr:10m;
server {
location /download {
limit_conn one 1;
limit_rate_after 1000k;
limit_rate 100k;
limit_conn client_addr 10; ##發數設置爲10
}
}
}
3、隱藏 Nginx 版本信息
版本信息關閉前
在配置文件上加
http{
server_tokens off;
}
版本信息關閉後