Nginx服務器

#########################

---Nginx服務器
   -->輕量級的http服務器
   -->反向代理服務器

##########################

---部署Nginx服務器

(1)使用源碼包安裝nginx軟件包

yum -y install gcc pcre-devel openssl-devel   //安裝依賴包

tar -xf nginx-1.8.0tar.gz    //解壓到當前目錄
cd nginx-1.8.0  //進入當前目錄
./configure \
>--prefix==/usr/local/nginx \   //指定安裝目錄
>--with-http_ssl_module     //開啓SSL加密功能

.....
.....
make    //編譯
make install   //安裝

echo $PATH
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/   //製作軟連接(快捷方式)
nginx    //啓動服務
nginx -s stop  //關閉服務
nginx -s reload //重新加載配置文件
nginx -V     //查看軟件信息

ls /usr/local/ngins
conf      --> 配置文件
sbin      --> 啓動程序
log       --> 服務日至
html      --> 網頁內容目錄

(2)升級新版本nginx軟件
tar -xf nginx-1.8.0tar.gz
cd nginx-1.8.0 
./configure \    
>..       \
>..                     //安裝其他功能
make
cp /nginx-1.8.0/objs/nginx /usr/local/nginx/sbin  //拷貝新版本
make upgrate   //升級

---nginx 製作網頁
【nginx】
修改nginx配置文件
/usr/local/nginx/conf/nginx.conf

server {
    listen 80;
    server_name localhost;
}
location / {
    root html;
    index index.html index.htm;
}

echo "192.168.4.5" > /usr/local/nginx/html/index.html

【客戶端】
firefox 192.168.4.5
192.168.4.5
########################

---加入用戶認證:
修改配置文件
/usr/local/nginx/conf/nginx.conf

server {
    listen 80;
    server_name localhost;
    auth_basic "Input Passwd";
    auth_basic_user_pass "/usr/local/nginx/pass";
}
location / {
    root html;
    index index.html index.htm;
}
pass 目錄不用手動創建 可使用 httpd-tools 軟件
yum -y install httpd-tools
htpasswd -c /usr/local/nginx/pass tom   //追加用戶 不用 -c
passwd :123456
passwd :123456
nginx -s reload

【客戶端】
firefox 192.168.4.5
user:tom
passwd:123456
192.168.4.5

##################################

     ---基於域名的虛擬主機

     修改配置文件

     http {
         server {
          listen 80;
          server_name www.a.com;

            location / {
            root html;
            index index.html;
                }
            }
          server {
             listen 80;
             server_name www.b.com;

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

         nginx -s reload

         mkdir /usr/local/nginx/web
         echo "www.b.com" > web/index.html

         【驗證】
         vim /etc/hosts
         www.a.com 192.168.4.5
         www.b.com 192.168.4.5

         firefox www.a.com
         firefox www.b.com

         #######################################

            -SSL 虛擬主機

            公鑰(證書)-- 用於加密
            私鑰 ---     用於解密

            openssl grnrsa > private.key   //生成私鑰
            openssl rep -new -x509 -key private.key > public.pem //生成公鑰
            修改配置文件

            usr/local/nginx/conf/nginx.conf

           server { 
                  listen 443;
                   server_name localhost;

                 ssl_certificate      public.pem;
                 ssl_certificate_key  private.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;
             }
          }

        nginx -t

        firefox https://192.168.4.5

        #######################################

        反向代理(調度服務器)(輪詢)

        修改配置文件

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

        http {

           upstream abc {
            #ip hash;   //根據客戶端IP分配固定的後端服務器
            server 192.168.4.100 #max_fails=1 fail_timeout=30;//最大失敗次數 1次,失敗後,暫
                                                停提供服務的時間
            server 192.168.4.200 #weight=2 down;//權重2 訪問此頁面2次 down 當前server不參與負
                                        載
          }
           server {

            ....
              location {
               proxy_pass http://abc
            ...
                }
                }
             }

             nginx -t

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