1. 程式人生 > >Nginx搭建,訪問控制

Nginx搭建,訪問控制

定時 exp mkdir 靜態頁面 stop oot type $? ali

一、Nginx

1.優點

1)多並發數:30000 - 50000

2)網易、騰訊等

3)新聞、論壇等

4)靜態網頁

5)輕量級

6)nginx+tomcat:負載均衡

Apache:模塊化設計

特點:

1)多並發數:30000-50000

2)模塊較少(緩存、群集)

3)輕量化(工作模式event)

二、Nginx

源代碼

1.安裝軟件包

1)rm -rf /etc/yum.repos.d/*

2)vim /etc/yum.repos.d/local.repo

[name]
name=local
baseurl=file:///mnt
gpgcheck=0
enable=1

3)yum -y install lrzsz //安裝軟件包,支持鼠標上傳文件

4)yum -y install pcre-devel //支持地址重寫功能(防盜鏈)

5)useradd -M -s /sbin/nologin nginx //新建運行用戶

6)tar -zxvf nginx-1.6.0.tar.gz -C /usr/src/ //解壓nginx源碼包

7)cd /usr/src/nginx-1.6.0/ //進入nginx源碼解壓目錄

8)./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

詳解:

--user:指定運行用戶

--group:指定運行組

--with-http_stub_status_module:啟用狀態統計模塊支持

8)make && make install

2.修改配置文件

vim /usr/local/nginx/conf/nginx.conf //編輯nginx主配置文件

user nobody nginx; //指定Nginx運行用戶和組
worker_processes 1; //啟動進程數(根據物理CPU個數設置)
error_log logs/error.log info; //定義錯誤日誌,記錄級別為info(信息)

pid logs/nginx.pid; //指定PID文件(存儲程序進程號)位置

events {
use epoll; //使用epoll網絡I/O模型,優化Nginx
worker_connections 1024; //每個工作進程允許最大的同時連接數
}

http {
include mime.types;

//額外加載該文件(mime.types內定義各文件類型映像,如image/png png;png格式文件為圖片類型;主要用於識別文件類型,什麽類型使瀏覽器用什麽方式呈現)

default_type  application/octet-stream; //默認響應為文件流

access_log  logs/access.log  main;          //指定所有站點訪問日誌存放路徑

sendfile        on;                 //打開系統函數sendfile()提高性能
tcp_nopush     on;                  //sendfile開啟後才生效,調用tcp_cork方法
#keepalive_timeout  0;
keepalive_timeout  65;              //會話保持時間,指定時間內客戶端無訪問請求,斷開連接,需連接時重新請求

gzip  on;                       //網頁壓縮

server {
    listen       80;                    //定義服務器監聽端口
    server_name  localhost;                 //定義服務器名及監聽IP

    charset utf-8;                  //網站的字符編碼

    access_log  logs/host.access.log  main;     //指定當前站點訪問日誌存放路徑
    location / {                        ////匹配客戶端所有請求,執行如下操作
        root   html;                    //網頁存放目錄
        index  index.html index.htm;            //Nginx首頁支持頁面
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {                  //錯誤頁面

    }

}

}

3.啟動服務

1)ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ 或echo "PATH=$PATH:/usr/local/nginx/sbin/" >>/etc/profile && source /etc/profile

//將命令做軟鏈接或加入到PATH環境變量,方便命令執行

2)vim /etc/init.d/nginx

#!/bin/bash

chkconfig: - 99 20

description: Nginx Server Control Script

NP="/usr/local/nginx/sbin/nginx"
NPF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$NP;
if [ $? -eq 0 ]
then
echo "nginx is starting!! "
fi
;;
stop)
kill -s QUIT $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx is stopping!! "
fi
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx config file is reload! "
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0

3)chmod +x /etc/init.d/nginx

4)/etc/init.d/nginx start && chkconfig --level 35 nginx on

5)nginx -t //檢查配置文件是否有誤

4.增加狀態統計支持

1)vim /usr/local/nginx/conf/nginx.conf

location  /status {         //在server下添加如下行
    stub_status on;
    access_log off;
}

2)/etc/init.d/nginx restart

3)http://192.168.1.10/status

5.aws狀態統計頁面

1)awstats部署

tar -zxvf awstats-7.3.tar.gz //解壓

mv awstats-7.3 /usr/local/awstats //移動並重命名為/usr/local/awstats目錄

chown -R root:root /usr/local/awstats //設置目錄所有者及所有組為root用戶

chmod -R 755 /usr/local/awstats/ //給予所有者完整權限

chmod +x /usr/local/awstats/tools/*.pl //給予所有以.pl結尾的文件所有人擁有執行權限

chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl //給予所有以.pl結尾的文件所有人擁有執行權限

2)awstats配置

cd /usr/local/awstats/tools/

./awstats_configure.pl //生成配置文件及目錄(y-->none-->y-->主機名-->回車-->回車)

vim /etc/awstats/awstats.www.xueluo.org.conf //編輯生成的配置文件

50 LogFile="/usr/local/nginx/logs/access.log" //修改Nginx訪問日誌路徑

mkdir /var/lib/awstats //創建圖表存放目錄

/usr/local/awstats/wwwroot/cgi-bin/awstats.pl --update --config=www.xueluo.org //根據日誌生成圖表

3)生成html靜態頁面

mkdir /usr/local/nginx/html/awstats //創建靜態頁面存放目錄

./awstats_buildstaticpages.pl --update --config=www.xueluo.org --lang=cn --dir=/usr/local/nginx/html/awstats/

//根據配置文件生成中文的html靜態文件到/usr/local/nginx/html/awstats/

vim /usr/local/nginx/conf/nginx.conf

39 location ~ ^/awstats {
40 root /usr/local/nginx/html/awstats;
41 index index.html;
42 }
43
44 location ~ ^/icon|/css|/js|/classess {
45 root /usr/local/awstats/wwwroot/;
46 }

crontab -e //新建計劃任務,每隔5分鐘生成圖表並轉換為html文件

*/5 * * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl --update --config=www.xueluo.org && /usr/local/awstats/tools/awstats_buildstaticpages.pl --update --config=www.xueluo.org --lang=cn --dir=/usr/local/nginx/html/awstats/  

4)訪問

http://IP/awstats.www.xueluo.org.html
一、訪問控制

1.生成密碼認證文件(htpasswd) yum -y install httpd-tools

1)rm -rf /etc/yum.repos.d/*

2)vim /etc/yum.repos.d/local.repo

[local]
name=local
baseurl=file:///mnt
gpgcheck=0

3)mount /dev/cdrom /mnt

4)yum -y install httpd-tools

5)htpasswd -c /usr/local/nginx/conf/.hehe hehe

6)chown nginx /usr/local/nginx/conf/.hehe && chmod 400 /usr/local/nginx/conf/.hehe

2.修改配置文件,添加認證選項

1)vim /usr/local/nginx/conf/nginx.conf

location /status { //Server配置項下增加
stub_status on;
access_log off;
auth_basic "secret"; //基本認證
auth_basic_user_file /usr/local/nginx/conf/.hehe; //指定用戶認證配置文件路徑
}

3.重啟服務,測試

1)/etc/init.d/nginx restart

二、虛擬主機

1.實現方式

1)基於域名:不同域名、相同IP、相同端口

2)基於IP:不同域名、不同IP、相同端口

3)基於端口:不同域名、不同IP、不同端口

三、基於域名

1.DNS搭建

1)安裝bind軟件包

rm -rf /etc/yum.repos.d/*

vim /etc/yum.repos.d/local.repo

[local]
name=local
baseurl=file:///mnt
gpgcheck=0

mount /dev/cdrom /mnt

yum -y install bind bind-utils

2)編輯配置文件

vim /etc/named.conf

options {
listen-on port 53 { 192.168.1.10; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursion yes;

dnssec-enable no;
dnssec-validation no;

}

zone "xueluo.org" IN {
type master;
file "xueluo.org.zone";
};

cp /var/named/named.empty /var/named/xueluo.org.zone

vim /var/named/xueluo.org.zone

$TTL 86400
@ IN SOA xueluo.org. root.xueluo.org. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
IN NS ns.xueluo.org.
ns IN A 192.168.1.10
www IN A 192.168.1.10
image IN A 192.168.1.10

3)啟動服務並測試

chown named:named /var/named/xueluo.org.zone

/etc/init.d/named restart

2.編輯nginx配置文件

1)vim /usr/local/nginx/conf/nginx.conf

111 server {
112 listen 80;
113 server_name www.xueluo.org;
114
115 location / {
116 root /usr/local/nginx/html/www;
117 index index.html;
118 }
119 }

121 server {
122 listen 80;
123 server_name image.xueluo.org;
124
125 location / {
126 root /usr/local/nginx/html/image;
127 index index.html;
128 }
129 }

2)mkdir /usr/local/nginx/html/www && mkdir /usr/local/nginx/html/image

3)echo "www is www" >/usr/local/nginx/html/www/index.html

4)echo "image is image" >/usr/local/nginx/html/image/index.html

3.啟動服務

1)nginx -t //驗證配置文件是否有誤

2)/etc/init.d/nginx restart

四、基於IP

1.DNS搭建

1)安裝bind軟件包

rm -rf /etc/yum.repos.d/*

vim /etc/yum.repos.d/local.repo

[local]
name=local
baseurl=file:///mnt
gpgcheck=0

mount /dev/cdrom /mnt

yum -y install bind bind-chroot bind-utils

2)編輯配置文件

vim /etc/named.conf

options {
listen-on port 53 { 192.168.1.10; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursion yes;

dnssec-enable no;
dnssec-validation no;

}

zone "xueluo.org" IN {
type master;
file "xueluo.org.zone";
};

cp /var/named/named.empty /var/named/xueluo.org.zone

vim /var/named/xueluo.org.zone

$TTL 86400
@ IN SOA xueluo.org. root.xueluo.org. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
IN NS ns.xueluo.org.
ns IN A 192.168.1.10
www IN A 192.168.1.10
image IN A 192.168.1.11

3)啟動服務並測試

ifconfig eth0:0 192.168.1.11

chown named:named /var/named/xueluo.org.zone

/etc/init.d/named restart

nslookup www.xueluo.org && nslookup image.xueluo.org

2.編輯nginx配置文件

1)vim /usr/local/nginx/conf/nginx.conf

36 listen 81; //將默認監聽端口換位81

111 server {
112 listen 192.168.1.10:80;
113 server_name www.xueluo.org;
114
115 location / {
116 root /usr/local/nginx/html/www;
117 index index.html;
118 }
119 }

121 server {
122 listen 192.168.1.20:80;
123 server_name image.xueluo.org;
124
125 location / {
126 root /usr/local/nginx/html/image;
127 index index.html;
128 }
129 }

2)mkdir /usr/local/nginx/html/www && mkdir /usr/local/nginx/html/image

3)echo "www is www" >/usr/local/nginx/html/www/index.html

4)echo "image is image" >/usr/local/nginx/html/image/index.html

3.啟動服務

1)nginx -t //驗證配置文件是否有誤

2)/etc/init.d/nginx restart

五、基於端口

1.DNS搭建

1)安裝bind軟件包

rm -rf /etc/yum.repos.d/*

vim /etc/yum.repos.d/local.repo

[local]
name=local
baseurl=file:///mnt
gpgcheck=0

mount /dev/cdrom /mnt

yum -y install bind bind-chroot bind-utils

2)編輯配置文件

vim /etc/named.conf

options {
listen-on port 53 { 192.168.1.10; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursion yes;

dnssec-enable no;
dnssec-validation no;

}

zone "xueluo.org" IN {
type master;
file "xueluo.org.zone";
};

cp /var/named/named.empty /var/named/xueluo.org.zone

vim /var/named/xueluo.org.zone

$TTL 86400
@ IN SOA xueluo.org. root.xueluo.org. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
IN NS ns.xueluo.org.
ns IN A 192.168.1.10
www IN A 192.168.1.10
image IN A 192.168.1.11

3)啟動服務並測試

ifconfig eth0:0 192.168.1.11

chown named:named /var/named/xueluo.org.zone

/etc/init.d/named restart

nslookup www.xueluo.org && nslookup image.xueluo.org

2.編輯nginx配置文件

1)vim /usr/local/nginx/conf/nginx.conf

36 listen 81; //將默認監聽端口換位81

111 server {
112 listen 192.168.1.10:82;
113 server_name www.xueluo.org;
114
115 location / {
116 root /usr/local/nginx/html/www;
117 index index.html;
118 }
119 }

121 server {
122 listen 192.168.1.20:83;
123 server_name image.xueluo.org;
124
125 location / {
126 root /usr/local/nginx/html/image;
127 index index.html;
128 }
129 }

2)mkdir /usr/local/nginx/html/www && mkdir /usr/local/nginx/html/image

3)echo "www is www" >/usr/local/nginx/html/www/index.html

4)echo "image is image" >/usr/local/nginx/html/image/index.html

3.啟動服務

1)nginx -t //驗證配置文件是否有誤

2)/etc/init.d/nginx restart

Nginx搭建,訪問控制