web:nginx基礎及搭建

Nginx簡介

  • Nginx(“engine X”)
    – 是俄羅斯人編寫的十分輕量級的HTTP服務器
    – 是一個高性能的HTTP和反向代理服務器,同時也是一個IMAP/POP3/SMTP 代理服務器
  • 官方網站:http://nginx.org/

Nginx軟件安裝

yum -y install gcc pcre-devel openssl-devel #安裝依賴
tar -xf nginx-1.10.3.tar.gz
useradd nginx -s /sbin/nologin
cd nginx-1.10.3 \
>./configure -->prefix=/user/local/nginx \  #默認安裝就是這個
>--user=nginx \ #默認啓動用戶是Nobody
>--group=nginx \
>--with-http_ssl_module   #支撐劑加密功能
make && make install

nginx查看默認安裝模塊

cat nginx-1.10.3/auto/options | grep YES #篩選出默認安裝模塊

Nginx進程管理

  • 啓動Nginx服務
/usr/local/nginx/sbin/nginx 
  • 常用選項
    – -V:查看編譯參數
    – -c:指定配置文件,啓動服務
    – -s stop:關閉服務
    – -s reload :重新加載配置文件
  • 日誌管理
ls /usr/local/nginx/logs/
access.log  error.log  nginx.pid
  • 查看服務相關進程及端口信息
 ps aux | grep nginx
root     31661  0.0  0.0  25004   776 ?        Ss   20:29   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx    31662  0.0  0.1  25420  1736 ?        S    20:29   0:00 nginx: worker process
root     31897  0.0  0.0 112704   972 ttyS0    S+   20:34   0:00 grep --color=auto nginx

ss -pantu  | grep nginx
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=31662,fd=6),("nginx",pid=31661,fd=6))

Nginx配置分析

  • 配置文件結構
全局配置:/usr/local/nginx/conf/nginx.conf
http{
	.....
	server{
			......
			location/{
					......
			          }
		   }
     } 
  • 配置容器
http{
.....
server{
	listen	80;
	server_name	localhost:
	localtion/{
		root html;
		index index.html index.htm;
      }
    }
 }		    

用戶認證

在這裏插入圖片描述
默認安裝模塊:ngx_http_auth_basic_module

  • 編輯配置文件
.. ..
server {
        listen       80;
        server_name  localhost;
        auth_basic "Input Password:";     #認證提示符信息
        auth_basic_user_file  "/usr/local/nginx/pass";   #認證的密碼文件
 		#也可以加載某個location{}裏面
        location / {
            root   html;
            index  index.html index.htm;
        }
  }	
  • 生成密碼文件,創建用戶及密碼
    使用htpasswd命令創建賬戶文件,需要確保系統中已經安裝了httpd-tools。
yum -y install  httpd-tools
htpasswd -c /usr/local/nginx/pass   tom     #創建密碼文件
New password: 
Re-type new password: 
Adding password for user tom

htpasswd  /usr/local/nginx/pass   jerry      //追加用戶,不使用-c選項
New password: 
Re-type new password: 
Adding password for user jerry

cat /usr/local/nginx/pass
  • 重新加載配置
/usr/local/nginx/sbin/nginx -s reload    //重新加載配置文件    
#請先確保nginx是啓動狀態,否則運行該命令會報錯,報錯信息如下:
#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

Nginx虛擬主機

三種模式虛擬主機

  • 基於域名的虛擬主機
  • 基於端口的虛擬主機
  • 基於IP的虛擬主機

基於域名的虛擬主機

  • 大致結構
server{	
		listen 80;
		server_name web1.lg.com;#域名1
		.....  
	   } 
server{
		listen 80:
		server_name web2.lg.com;#域名2
		....
      }
  • 詳細修改配置文件
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;                                      //端口
        server_name  www.a.com;                                //域名
auth_basic "Input Password:";                        //認證提示符
        auth_basic_user_file "/usr/local/nginx/pass";        //認證密碼文件
location / {
            root   html;                                    //指定網站根路徑
            index  index.html index.htm;
       }     
}
… …
    server {
        listen  80;                                        //端口
        server_name  www.b.com;                                //域名
location / { 
root   www;                                 //指定網站根路徑
index  index.html index.htm;
}
}
  • 創建網站根目錄及對應首頁文件
mkdir /usr/local/nginx/www
echo "www" > /usr/local/nginx/www/index.html
  • 客戶端測試
修改客戶端主機的/etc/hosts文件,進行域名解析
vim /etc/hosts
192.168.4.5    www.a.com  www.b.com
登錄客戶端主機進行測試
firefox http://www.a.com    #輸入密碼後可以訪問
firefox http://www.b.com    #直接訪問

基於端口的虛擬主機

server{
		listen 8080; #端口1
		server_name web1.lg.com; #域名
		......
	   }
server{
		listen  8000; #端口2
		server_name web1.lg.com;
		....
		}

基於IP的虛擬主機

server{
		listen 192.168.0.1:80; #端口
		server_name web1.lg.com; #域名
		.....
		}
server{
		listen 192.168.0.2:80;
		server_name web1.lg:com;
		.....
		} 

SSL虛擬主機

源碼安裝Nginx時必須使用–with-http_ssl_module參數,啓用加密模塊,對於需要進行SSL加密處理的站點添加ssl相關指令(設置網站需要的私鑰和證書)。
加密算法一般分爲對稱算法、非對稱算法、信息摘要。
對稱算法有:AES、DES,主要應用在單機數據加密。
非對稱算法有:RSA、DSA,主要應用在網絡數據加密。
信息摘要:MD5、sha256,主要應用在數據完整性校驗。

  • 生成私鑰與證書
cd /usr/local/nginx/conf
openssl genrsa > cert.key #生成私鑰
openssl req -new -x509 -key cert.key > cert.pem      #生成證書
  • 修改Nginx配置文件,設置加密網站的虛擬主機
vim  /usr/local/nginx/conf/nginx.conf
… …    
server {
        listen       443 ssl;
        server_name          www.c.com;
        ssl_certificate      cert.pem;    #這裏是證書文件
        ssl_certificate_key  cert.key;    #這裏是私鑰文件
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
  • 重新加載配置
/usr/local/nginx/sbin/nginx -t #檢測配置文件是否正確
/usr/local/nginx/sbin/nginx -s reload
#請先確保nginx是啓動狀態,否則運行該命令會報錯,報錯信息如下:
#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章