nginx服務網站

Nginx簡介:

   Nginx是一款專爲性能優化而開發的, http 和反向代理服務器,其最知名的優點就是它的穩定性和低系統資源消耗,由俄羅斯人Igor Sysoev(伊戈爾·賽索耶夫)爲俄羅斯網站Rambler.ru開發的,在Rambler.ru網站平穩的運行了四年,而且俄俄羅斯超過20%的虛擬主機平臺採用Nginx作爲反向代理服務器。

優點:內存消耗少:處理靜態文件,比apache 佔用更少的內存及資源

缺點:動態處理差:nginx處理靜態文件較好,耗費內存少,但是動態沒有apache做的好

一般來說前端用nginx作爲反向代理抗住壓力,後端使用apache處理動態請求。

 

安裝及運行控制 

Nginx官網http://nginx.org/下載

網盤https://pan.baidu.com/s/1Ei3cVU-hG_zKORXzi3Z7yQ下載

1.編譯安裝

1)安裝前提軟件

[root@mysql ~]# yum -y install pcre-devel zlib-devel

2)創建運行用戶和組

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

3)編譯安裝

[root@mysql ~]# cd /root/Desktop/
[root@mysql Desktop]# tar zxf nginx-1.12.0.tar.gz -C /usr/src/
[root@mysql Desktop]# cd /usr/src/
[root@mysql src]# cd nginx-1.12.0/
[root@mysql nginx-1.12.0]#  ./configure --prefix=/usr/local/nginx   \ 
--user=nginx     \     //指定用戶
--group=nginx    \    //指定組
--with-http_stub_status_module  \  //模塊以支持狀態統計
 && make && make install

4)鏈接優化路徑

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

2.運行控制

[root@mysql ~]# nginx             //啓動服務
[root@mysql ~]# netstat -anput | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6400/nginx: master  
[root@mysql ~]# yum -y install psmisc        //安裝killall命令

[root@mysql ~]#killall -s QUIT nginx     //關閉Nginx

[root@mysql ~]#	killall -s HUP nginx      //重啓Nginx

[root@mysql ~]#	nginx -t   // 檢測配置文件

 

配置文件nginx.conf

主配置文件位於/usr/local/nginx/conf/nginx.conf

1.全局配置

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#user  nginx;
                                        //運行用戶
worker_processes  1;                   //工作進程數量
 
#error_log  logs/error.log;           //錯誤日誌文件的位置
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;           //PID文件存放位置

2.I/O事件配置

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

evets {
    use epoll;                   //使用epoll模型
    worker_connections  1024   //每個進程處理1024個連接
}

3.http配置

http {
    include       mime.types;
    default_type  application/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;        //訪問日誌位置

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;                //連接保持超時

    #gzip  on;

    server {                            //web服務監聽配置
        listen       80;                //監聽地址及端口
        server_name  localhost;            //網站名稱

        #charset koi8-r;          //默認字符集
        #access_log  logs/host.access.log  main;

        location / {                //根目錄配置
            root   html;            //根目錄位置
            index  index.html index.htm;   //默認首頁(索引頁)
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;         //錯誤反饋信息
        location = /50x.html {              //錯誤頁面配置
            root   html;
        }

訪問狀態及虛擬主機

1.訪問統計

Nginx內置了HTTP_STUB_STATUS狀態統計模塊,用來反饋當前的Web訪問情況,在編譯安裝Nginx時添加--with-http_stub_status_module來啓用此模塊。

 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

location /status {       //訪問位置爲status
stub_status on;           //打開狀態統計功能
access_log off;          //關閉此位置的日誌記錄

}

測試

Active connections,表示當前網頁連接數。
server accepts handled requests,表示已處理的連接信息。
224表示,已處理的請求數。

 2.Nginx虛擬主機

  基於域名的虛擬Web主機通過域名區分不同的Web站點。使用Nginx搭建虛擬主機服務器時,每個虛擬Web站點擁有獨立的“server{}”配置段,各自的監聽IP地址,端口號等可以單獨指定。

1)配置dns服務,使用兩個域名解析同一個ip

[root@mysql ~]# yum -y install bind bind-chroot
[root@mysql etc]# mv named.conf named.conf.bak  //備份
[root@mysql etc]# vim named.conf

options {
        	directory "/var/named";
	};
	zone "benet.com" {
        	type master;
        	file "benet.com.zone";
	};
	zone "accp.com" {
        	type master;
        	file "accp.com.zone";
	};

[root@mysql etc]# cd /var/named/
[root@mysql named]# cp -p named.localhost benet.com.zone
[root@mysql named]# vim benet.com.zone    //添加benet的a記錄
$TTL 1D 
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
        AAAA    ::1
www     A    192.168.1.1

[root@mysql named]# cp -p benet.com.zone accp.com.zone   
//因爲是使用不同域名相同ip所以複製一下就行
[root@mysql named]# cat accp.com.zone  //驗證是否爲相同ip
$TTL 1D
@	IN SOA	@ rname.invalid. (
					0	; serial
					1D	; refresh
					1H	; retry
					1W	; expire
					3H )	; minimum
	NS	@
	A	127.0.0.1
	AAAA	::1
www	A	192.168.1.1


[root@mysql named]# systemctl start named   //開啓服務
[root@mysql named]# netstat -anput | grep named
tcp        0      0 192.168.1.1:53          0.0.0.0:*               LISTEN      45089/named         
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      45089/named         
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      45089/named         
tcp6       0      0 ::1:953                 :::*                    LISTEN      45089/named         
udp        0      0 192.168.122.1:53        0.0.0.0:*                           45089/named         
udp        0      0 192.168.1.1:53          0.0.0.0:*                           45089/named         
udp        0      0 127.0.0.1:53           


2)設置測試文件首頁

[root@mysql ~]# mkdir -p /wwwroot/benet  
[root@mysql ~]# mkdir -p /wwwroot/accp
[root@mysql ~]# echo "benet.com" >/wwwroot/benet/index.html
[root@mysql ~]# echo "accp.com" >/wwwroot/accp/index.html

3)調整nginx.conf配置文件

[root@mysql ~]# vim /usr/local/nginx/conf/nginx.conf 
server {
        listen       80;
        server_name  www.benet.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /wwwroot/benet;   //benet的根目錄
            index  index.html index.htm;
        }
}

 server {
        listen       80;
        server_name  www.accp.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /wwwroot/accp;   //accp的根目錄
            index  index.html index.htm;
        }
}
[root@mysql ~]# nginx -t
[root@mysql ~]# killall -s HUP nginx   //重啓

 

測試

注意配置虛擬主機時的‘’{}‘’號是否相對。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章