1. 程式人生 > >centos中編譯安裝nginx並支援ssl

centos中編譯安裝nginx並支援ssl

安裝編譯環境和必要的庫

[root@localhost ~]# yum -y install gcc gcc-c++ autoconf automake libtool make cmake
[root@localhost ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

說明

zlib: 為nginx提供gzip模組,需要zlib庫支援
openssl: 為nginx提供ssl功能
pcre: 為支援地址重寫rewrite功能,有一些作業系統使用yum install pcre-devel安裝,由於缺乏包,而安裝失敗,因此需要手動安裝

手動安裝pcre

[root@localhost ~]#wget https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.gz
[root@localhost ~]#tar -zxvf pcre-8.39.tar.gz
[root@localhost ~]#cd pcre-8.39
[root@localhost ~]#./configure
[root@localhost ~]#make
[root@localhost ~]#make install

建立用來執行nginx的使用者及組

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

說明

-g引數為nginx使用者指定了一個組。
-M引數保證其不自動生成home目錄。

編譯安裝Nginx

[root@localhost ~]#wget http://nginx.org/download/nginx-1.10.1.tar.gz
[root@localhost ~]#ls
[root@localhost ~]#tar -zxvf nginx-1.10.1.tar.gz 
[root@localhost ~]#cd nginx-1.10.1
[root@localhost ~]#ls [root@localhost ~]#./configure --prefix=/usr/local/nginx --with-http_ssl_module --user=nginx --group=nginx --with-pcre [root@localhost ~]#make [root@localhost ~]#make install

啟動

[root@localhost ~]# /usr/local/nginx/sbin/nginx   啟動
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop  停止
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload  重新載入
[root@localhost ~]# /usr/local/nginx/sbin/nginx -v  檢視版本
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t  測試配置檔案是否正常
[root@localhost ~]# pkill nginx  強制關閉

檢視安裝狀態

[[email protected] ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.10.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --user=nginx --group=nginx --with-pcre

nginx配置檔案

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

Nginx配置檔案常見結構的從外到內依次是「http」「server」「location」等等,預設的繼承關係是從外到內,也就是說內層塊會自動獲取外層塊的值作為預設值。

Server
接收請求的伺服器需要將不同的請求按規則轉發到不同的後端伺服器上,在 nginx 中我們可以通過構建虛擬主機(server)的概念來將這些不同的服務配置隔離。

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

說明

listen 指監聽埠
server_name 用來指定IP或域名,多個域名對應統一規則可以空格分開
root 指令用於指定虛擬主機的網頁跟目錄,這個地方可以是相對地址也可以是絕對地址
index 用於設定訪問的預設首頁地址

通常情況下我們可以在 nginx.conf 中配置多個server,對不同的請求進行設定。例如配置兩個server,servername分別是test1.euming.com 和 test2.euming.com ,就像這樣:

server {
    listen       80;
    server_name  test1.euming.com;
    root   html;
    index  index.html index.htm;
}
server {
    listen       80;
    server_name  test2.euming.com;
    root   /data/www/html;
    index  index.html index.htm;
}

但是當 server 超過2個時,建議將不同對虛擬主機的配置放在另一個檔案中,然後通過在主配置檔案 nginx.conf 加上 include 指令包含進來。更便於管理。

include vhosts/*.conf;

就可以把vhosts的檔案都包含進去啦。

Localtion

每個 url 請求都會對應的一個服務,nginx 進行處理轉發或者是本地的一個檔案路徑,或者是其他伺服器的一個服務路徑。而這個路徑的匹配是通過 location 來進行的。我們可以將 server 當做對應一個域名進行的配置,而 location 是在一個域名下對更精細的路徑進行配置。

以上面的例子,可以將root和index指令放到一個location中,那麼只有在匹配到這個location時才會訪問root後的內容:

附錄:nginx編譯引數說明如下:

--prefix=<path> -- 安裝路徑,如果沒有指定,預設為/usr/local/nginx。
--sbin-path=<path> -- nginx可執行命令的檔案,如果沒有指定,預設為<prefix>/sbin/nginx--conf-path=<path> -- 在沒有使用-c引數指定的情況下nginx.conf的預設位置,如果沒有指定,預設為<prefix>/conf/nginx.conf。
--pid-path=<path> -- nginx.pid的路徑,如果沒有在nginx.conf中通過“pid”指令指定,預設為<prefix>/logs/nginx.pid。
--lock-path=<path> -- nginx.lock檔案路徑,如果沒有指定,預設為<prefix>/logs/nginx.lock。
--error-log-path=<path> -- 當沒有在nginx.conf中使用“error_log”指令指定時的錯誤日誌位置,如果沒有指定,預設為<prefix>/logs/error.log。
--http-log-path=<path> -- 當沒有在nginx.conf中使用“access_log”指令指定時的訪問日誌位置,如果沒有指定,預設為<prefix>/logs/access.log。
--user=<user> -- 當沒有在nginx.conf中使用“user”指令指定時nginx執行的使用者,如果沒有指定,預設為“nobody”。
--group=<group> -- 當沒有在nginx.conf中使用“user”指令指定時nginx執行的組,如果沒有指定,預設為“nobody”。
--builddir=DIR -- 設定構建目錄。
--with-rtsig_module -- 啟用rtsig模組。
--with-select_module --without-select_module -- 如果在configure的時候沒有發現kqueue, epoll, rtsig或/dev/poll其中之一,select模組始終為啟用狀態。
--with-poll_module --without-poll_module -- 如果在configure的時候沒有發現kqueue, epoll, rtsig或/dev/poll其中之一,poll模組始終為啟用狀態。
--with-http_ssl_module -- 啟用ngx_http_ssl_module,啟用SSL支援並且能夠處理HTTPS請求。需要OpenSSL,在Debian系統中,對應的包為libssl-dev。
--with-http_realip_module -- 啟用ngx_http_realip_module
--with-http_addition_module -- 啟用ngx_http_addition_module
--with-http_sub_module -- 啟用ngx_http_sub_module
--with-http_dav_module -- 啟用ngx_http_dav_module
--with-http_flv_module -- 啟用ngx_http_flv_module
--with-http_stub_status_module -- 啟用”server status”(服務狀態)頁
--without-http_charset_module -- 禁用ngx_http_charset_module
--without-http_gzip_module -- 禁用ngx_http_gzip_module,如果啟用,需要zlib包。
--without-http_ssi_module -- 禁用ngx_http_ssi_module
--without-http_userid_module -- 禁用ngx_http_userid_module
--without-http_access_module -- 禁用ngx_http_access_module
--without-http_auth_basic_module -- 禁用ngx_http_auth_basic_module
--without-http_autoindex_module -- 禁用ngx_http_autoindex_module
--without-http_geo_module -- 禁用ngx_http_geo_module
--without-http_map_module -- 禁用ngx_http_map_module
--without-http_referer_module -- 禁用ngx_http_referer_module
--without-http_rewrite_module -- 禁用ngx_http_rewrite_module。如果啟用,需要PCRE包。
--without-http_proxy_module -- 禁用ngx_http_proxy_module
--without-http_fastcgi_module -- 禁用ngx_http_fastcgi_module
--without-http_memcached_module -- 禁用ngx_http_memcached_module
--without-http_limit_zone_module -- 禁用ngx_http_limit_zone_module
--without-http_empty_gif_module -- 禁用ngx_http_empty_gif_module
--without-http_browser_module -- 禁用ngx_http_browser_module
--without-http_upstream_ip_hash_module -- 禁用ngx_http_upstream_ip_hash_module
--with-http_perl_module -- 啟用ngx_http_perl_module
--with-perl_modules_path=PATH -- 為perl模組設定路徑
--with-perl=PATH -- 為perl庫設定路徑
--http-client-body-temp-path=PATH -- 為http連線的請求實體臨時檔案設定路徑,如果沒有指定,預設為<prefix>/client_body_temp
--http-proxy-temp-path=PATH -- 為http代理臨時檔案設定路徑,如果沒有指定,預設為<prefix>/proxy_temp
--http-fastcgi-temp-path=PATH - 為http fastcgi臨時檔案設定路徑,如果沒有指定,預設為<prefix>/fastcgi_temp
--without-http -- 禁用HTTP服務
--with-mail -- 啟用IMAP4/POP3/SMTP代理模組
--with-mail_ssl_module -- 啟用ngx_mail_ssl_module
--with-cc=PATH -- 設定C編譯器路徑
--with-cpp=PATH -- 設定C前處理器路徑
--with-cc-opt=OPTIONS -- 變數CFLAGS中附加的引數,用於FreeBSD中的PCRE庫,同樣需要指定--with-cc-opt=”-I /usr/local/include”,如果我們使用select()函式則需要同時增加檔案描述符數量,可以通過--with-cc-opt=”-D FD_SETSIZE=2048”指定。
--with-ld-opt=OPTIONS -- 通過聯結器的附加引數,用於FreeBSD中的PCRE庫,同樣需要指定--with-ld-opt=”-L /usr/local/lib”。
--with-cpu-opt=CPU -- 指定編譯的CPU,可用的值為: pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
--without-pcre -- 禁用PCRE庫檔案,同時將禁用HTTP rewrite 模組,如果要在”location”指令中使用正則表示式,同樣需要PCRE庫。
--with-pcre=DIR -- 設定PCRE庫原始檔路徑。
--with-pcre-opt=OPTIONS -- 在編譯時為PCRE設定附加引數。
--with-md5=DIR -- 設定md5庫原始檔路徑。
--with-md5-opt=OPTIONS -- 在編譯時為md5設定附加引數。
--with-md5-asm -- 使用md5彙編源。
--with-sha1=DIR -- 設定sha1庫原始檔路徑。
--with-sha1-opt=OPTIONS -- 在編譯時為sha1設定附加引數。
--with-sha1-asm -- 使用sha1彙編源。
--with-zlib=DIR -- 設定zlib庫原始檔路徑。
--with-zlib-opt=OPTIONS -- 在編譯時為zlib設定附加引數。
--with-zlib-asm=CPU -- 為指定的CPU使用zlib彙編源進行優化,可用值為: pentium, pentiumpro。
--with-openssl=DIR -- 設定openssl庫原始檔路徑。
--with-openssl-opt=OPTIONS -- 在編譯時為openssl設定附加引數。
--with-debug -- 啟用debug記錄。
--add-module=PATH -- 增加一個在PATH中的第三方模組。