Linux上nginx安裝配置(配置虛擬主機,靜態資源)

1.下載

進入http://nginx.org/en/download.html 下載nginx1.8.0版本(當前最新穩定版本)。

2.安裝

安裝前準備

nginx是C語言開發,建議在linux上運行,本教程使用Centos6.5作爲安裝環境。

2.1 gcc

       安裝nginx需要先將官網下載的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc:
yum install gcc-c++

2.2 PCRE

       PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。

2.3 zlib

       zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
yum install -y zlib zlib-devel

2.4 openssl

       OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。
       nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。
yum install -y openssl openssl-devel

2.5開始安裝

(1)導入nginx安裝包
(a)Rz命令導入nginx安裝包到usr/local/hadoop目錄下面。
(2)解壓:tar –zxvf nginx-1.8.0.tar.gz
(3)設置安裝參數
設置前準備
(a)--prefix:設置nginx安裝路徑
創建名令:mkdir -p /usr/local/nginx
(b)/var/temp/nginx目錄需要手動創建
創建名令:mkdir -p /var/temp/nginx
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
(4)編譯
編譯命令:make
(5)安裝
安裝命令:make install
(6)啓動
(a)進入nginx安裝目錄sbin
(b)./nginx
(7)訪問nginx

3.Nginx命令

3.1 Nginx啓動流程

Nginx啓動時候默認加載安裝目錄conf/nginx.conf
也可以指定加載配置路徑:./nginx –c /usr/local/hadoop/nginx-1.8.0/conf/nginx.conf

3.2 啓動命令

1)./nginx
2)./nginx –s reload 重啓

3.3 停止命令

1)./nginx –s stop
解析:強制終止nginx進程。
2)./nginx –s quit
解析:凌遲處死,先讓nginx把任務完成,然後終止。

4.nginx.conf配置

4.1基本參數

#user  nobody;
worker_processes  1;  //全局塊
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;   //events
}
http {   //http
    include       mime.types;
    default_type  application/octet-stream; 
    sendfile        on;
    keepalive_timeout  65;
    server {  //server
        listen       80;
        server_name  localhost;
       location / { //資源路徑塊
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
全局塊:配置影響nginx全局參數,配置工作進程數。默認1
Events:工作連接數,默認1024.
http塊:配置多個Server,配置虛擬主機,負載均衡等等。
Server塊:配置服務器,服務器端口,服務器IP地址。
Location塊:定位資源文件位置。

4.2基於域名的虛擬主機配置

4.2.1在local host中直接配置服務路徑



4.2.2另一種配置方式upstream

http {
    include       mime.types;
    default_type  application/octet-stream; 
    sendfile        on;
    keepalive_timeout  65;
   upstream mytomcat1{
       server 192.168.66.66:8080;
    }
    upstream mytomcat2{
       server 192.168.66.66:8081;
    }
    server {
        listen       80;
       server_name  www.jd.com;
        location / {
            #root   html;
            #index  index.html index.htm;
          proxy_pass http://mytomcat1;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
1)客戶機發送請求www.jd.com
2)Nginx接受請求:通過nginx.conf配置主機Server地址匹配:www.jd.com
3)匹配成功:就會去location下面尋找需要資源。
4)在location配置代理:去尋找代理服務器。http://mytomcat1
5)尋找名稱是mytomcat1的upstream,Upstream負責把請求轉發tomcat1。
通過端口訪問的tomcat只會訪問到tomcat的頁面,需要在tomcat中加入初始化頁面。
webapps/ROOT/index.jsp

4.3配置nginx代理靜態資源






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