歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux-Centos-7-64位:5、 nginx代理

linux-Centos-7-64位:5、 nginx代理

日期:2017/3/1 12:03:03   编辑:關於Linux

nginx 可以使用各平台的默認包來安裝,本文是介紹使用源碼編譯安裝,包括具體的編譯參數信息。

准備工作:

1、安裝必備工具:

編譯環境gcc g++ 開發庫之類的需要提前裝好

$ yum -y install gcc gcc-c++ autoconf automake

安裝過程中如果出現如下圖錯誤:

這裡寫圖片描述

原因:
dns服務器沒有配置正確
解決辦法:
①、打開文件/etc/resolv.conf在其中添加:<喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;"> nameserver 8.8.8.8 nameserver 8.8.4.4 search localdomain

這裡寫圖片描述

②、然後重啟網卡:使用命令: service network restart

如果是在虛擬機配置,那要把路由設置成和主機一致

這裡寫圖片描述

$ yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

說明:

pcre: 用來作地址重寫的功能。

zlib:nginx 的gzip模塊,傳輸數據打包,省流量(但消耗資源)。

openssl:提供ssl加密協議。

也可以先在線下下載:

pcre:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

zlib:http://zlib.net/

openssl:http://www.openssl.org/source/

2、新建一個系統級用戶組和匿名用戶,以及下面編譯時使用

$ sudo groupadd -r nginx

$ sudo useradd -s /sbin/nologin -g nginx -r nginx-user

查看用戶組及其成員

groups nginx-user

這裡寫圖片描述

Nginx編譯安裝:

1、下載Nginx:http://nginx.org/en/download.html

在 usr/local/src 目錄下創建 nginx 文件夾

mkdir nginx

將下載好的 nginx-1.9.15.tar.gz 放到 /mnt/share資源目錄並復制到 /usr/local/src/nginx 文件夾下並解壓,並進入解壓後的nginx-1.9.15文件夾下

cp /mnt/share/nginx-1.9.15.tar.gz /usr/local/nginx/
tar -zxvf nginx-1.9.15.tar.gz
cd nginx-1.9.15/

這裡寫圖片描述

./configure 參數配置信息詳情,可以通過 –help 查詢

$ ./configure \                    #nginx啟動
--prefix=/usr/local/nginx \             # 設定安裝目錄,默認使用 /usr/local/nginx。
--sbin-path=/usr/local/nginx/nginx \    # 設置nginx的可執行文件的路徑,默認為prefix/sbin/nginx.
--conf-path=/etc/nginx/nginx.conf \     # 設置在nginx.conf配置文件的路徑。nginx允許使用不同的配置文件啟動,通過命令行中的-c選項。默認為prefix/conf/nginx.conf.
--error-log-path=/var/log/nginx/error.log \     # 設置主錯誤,警告,和診斷文件的名稱。安裝完成後,可以隨時改變的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默認情況下,文件名 為prefix/logs/error.log.
--http-log-path=/var/log/nginx/access.log \     # 設置主請求的HTTP服務器的日志文件的名稱。安裝完成後,可以隨時改變的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默認情況下,文件名 為prefix/logs/access.log.
--pid-path=/var/run/nginx.pid \         # 設置nginx.pid文件,將存儲的主進程的進程號。安裝完成後,可以隨時改變的文件名 , 在nginx.conf配置文件中使用 PID指令。默認情況下,文件名 為prefix/logs/nginx.pid.
--lock-path=/var/run/nginx.lock \       # 設定lock文件(nginx.lock)目錄
--http-client-body-temp-path=/var/cache/nginx/client_temp \ # 設置客戶端請求臨時文件路徑
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \        # 設置http proxy臨時文件路徑
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \    # 設置http fastcgi臨時文件路徑
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \        # 設置路徑存儲http uwsgi臨時文件路徑
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \          # 設置路徑存儲http scgi臨時文件路徑
--user=nginx \      #  設置nginx工作進程的用戶。安裝完成後,可以隨時更改的名稱在nginx.conf配置文件中 使用的 user指令。默認的用戶名是nobody。
--group=nginx \     # 設置nginx工作進程的用戶組。安裝完成後,可以隨時更改的名稱在nginx.conf配置文件中 使用的 user指令。默認的為非特權用戶。
--with-http_ssl_module \        # 使用https協議模塊。默認情況下,該模塊沒有被構建。建立並運行此模塊的OpenSSL庫是必需的。
--with-http_realip_module \     # 允許ngx_http_realip_module模塊(mod_rpaf)
--with-http_addition_module \   # ..模塊(mod_layout)
--with-http_sub_module \        # ..模塊
--with-http_dav_module \        # ..模塊(mod_dav)
--with-http_flv_module \        # ..(mod_flvx)
--with-http_mp4_module \        # ..模塊(mod_mp4)
--with-http_gunzip_module \     # ..模塊(mod_gunzip)
--with-http_gzip_static_module \        # ..模塊(mod_dflate)
--with-http_random_index_module \       # ..模塊(mod_autoindex)
--with-http_secure_link_module \        # ..模塊
--with-http_stub_status_module \        # ..模塊(mod_status)通過web界面查看時Nginx需要開啟status模塊
--with-http_auth_request_module \       # 允許ngx_http_auth_request_module模塊
--with-http_xslt_module         # 允許ngx_http_xslt_module模塊
--with-mail \       # 允許POP3/IMAP4/SMTP代理模塊
--with-mail_ssl_module \        # 允許ngx_mail_ssl_module模塊
--with-file-aio \   # 允許文件AIO支持
--with-ipv6 \       # 允許IP6代理支持
--with-http_spdy_module \   # 允許ngx_http_spdy_module模塊1.9.5開始要把把--with-http_spdy_module換成--with-http_v2_module,不然報錯
--with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'  # 設置C編譯器參數
--without-http_charset_module       # 不使用ngx_http_charset_module模塊
--without-http_gzip_module          # 不使用ngx_..模塊
--without-http_ssi_module           # 不使用ngx_..模塊
--without-http_userid_module        # 不使用ngx_..模塊
--without-http_access_module        # 不使用ngx_..模塊
--without-http_auth_basic_module    # 不使用ngx_..模塊
--without-http_autoindex_module     # 不使用ngx_..模塊
--without-http_geo_module           # 不使用ngx_..模塊
--without-http_map_module           # 不使用ngx_..模塊
--without-http_referer_module       # 不使用ngx_..模塊
--without-http_rewrite_module       # 不使用ngx_..模塊
--without-http_proxy_module         # 不使用ngx_..模塊
--without-http_fastcgi_module       # 不使用ngx_..模塊
--without-http_memcached_module     # 不使用ngx_..模塊
--without-http_limit_zone_module    # 不使用ngx_..模塊
--without-http_empty_gif_module     # 不使用ngx_..模塊
--without-http_browser_module       # 不使用ngx_..模塊
--without-http_upstream_ip_hash_module      # 不使用ngx_..模塊
--with-http_perl_module         # 允許ngx_..模塊
--with-perl_modules_path=PATH   # 設置perl模塊路徑
--with-perl=PATH        # 設置perl庫文件路徑
--http-log-path=PATH    # 設置access log文件路徑
--without-http          # 不使用HTTP server功能
--without-mail_pop3_module      # 不允許ngx_..模塊
--without-mail_imap_module      # 不允許ngx_..模塊
--without-mail_smtp_module      # 不允許ngx_..模塊
--with-google_perftools_module      # 允許ngx_..模塊(調試用)
--with-cpp_test_module  # 允許ngx_..模塊
--add-module=PATH       # 允許使用外部模塊,以及路徑
--with-cc=PATH          # 設置C編譯器路徑
--with-cpp=PATH         # 設置C預處理路徑
--with-ld-opt=OPTIONS   # 設置連接文件參數
--with-cpu-opt=CPU      # 為指定CPU優化,可選參數有:
          pentium, pentiumpro, pentium3, pentium4,
          athlon, opteron, sparc32, sparc64, ppc64
--without-pcre          # 不使用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     # 使zlib對特定的CPU進行優化,可選參數:
          pentium, pentiumpro
--with-openssl=DIR      # 設定OpenSSL庫文件路徑
--with-openssl-opt=OPTIONS  # 設置OpenSSL運行參數
--with-debug            # 允許調試日志
--builddir=DIR          # 設定程序編譯目錄
--with-rtsig_module     # 允許rtsig模塊
--with-select_module    # 允許select模塊(一種輪詢模式,不推薦用在高載環境)
--without-select_module # 不使用select模塊
--with-poll_module      # 允許poll模塊(一種輪詢模式,不推薦用在高載環境)
--without-poll_module   # 不使用poll模塊

nginx編譯時不是功能加的越多越好,應該盡可能少編譯模塊,不用的最好不要加入。
編譯好的nginx可通過 /usr/local/nginx/sbin/nginx -V 查看編譯時的參數(具體路徑更具實際情況而定)

現將下列參數加入

./configure 
--sbin-path=/usr/local/nginx/nginx \    #文件夾要自己創建
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-http_stub_status_module \    

隨後執行

make && make install

啟動 nginx ,進入 /usr/local/nginx/sbin 目錄 運行 並查看是否啟動

./nginx
ps -ef | grep nginx

啟動成功

這裡寫圖片描述

重啟nginx

cd /usr/local/nginx/sbin
./nginx -s reload

判斷配置文件是否正確

cd  /usr/local/nginx/sbin
./nginx -t

關閉

  查詢nginx主進程號

  ps -ef | grep nginx

  從容停止 kill -QUIT 主進程號

  快速停止 kill -TERM 主進程號

  強制停止 kill -9 nginx

  若nginx.conf配置了pid文件路徑,如果沒有,則在logs目錄下

  kill -信號類型 ‘/usr/local/nginx/logs/nginx.pid’

升級

  1、先用新程序替換舊程序文件

  2、kill -USR2 舊版程序的主進程號或者進程文件名

    此時舊的nginx主進程會把自己的進程文件改名為.oldbin,然後執行新版nginx,此時新舊版本同時運行

  3、kill -WINCH 舊版本主進程號

  4、不重載配置啟動新/舊工作進程

    kill -HUP 舊/新版本主進程號

    從容關閉舊/新進程

    kill -QUIT 舊/新進程號

    快速關閉舊/新進程

    kill -TERM 舊/新進程號

Copyright © Linux教程網 All Rights Reserved