1. 程式人生 > >linux centos7 nginx 安裝部署和配置

linux centos7 nginx 安裝部署和配置

php strong b- 資料 解壓 停止 forward wsdl 安裝路徑

1/什麽是Nginx
Nginx("enginex")是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器,在高連接並發的情況下Nginx是Apache服務器不錯的替代品.其特點是占有內存少,並發能力強,事實上nginx的並發能力確實在同類型的網頁服務器中表現較好.目前中國大陸使用nginx網站用戶有:新浪、網易、騰訊,另外知名的微網誌Plurk也使用nginx。
Nginx作為負載均衡服務器,既可以在內部直接支持Rails和PHP程序對外進行服務,也可以支持作為HTTP代理服務器對外進行服務。Nginx采用C進行編寫,不論是系統資源開銷還是CPU使用效率都比Perlbal要好很多。
Nginx作為郵件代理服務器,是一個非常優秀的郵件代理服務器(最早開發這個產品的目的之一也是作為郵件代理服務器)。
Nginx是一個安裝非常簡單,配置文件非常簡潔(還能夠支持perl語法),Bugs非常少的服務器;Nginx啟動特別容易,並且幾乎可以做到7*24不間斷運行,即使運行數個月也不需要重新啟動,還能夠不間斷服務的情況下進行軟件版本的升級。

2Nginx的安裝


2.1Nginx安裝包下載
Nginx包下載地址:
http://nginx.org/download/nginx-1.8.0.tar.gz

Nginx依賴包下載地址:
1.gzip模塊需要zlib庫(在http://www.zlib.net/下載http://zlib.net/zlib-1.2.8.tar.gz)
2.rewrite模塊需要pcre庫(在http://www.pcre.org/下載ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/)
3.ssl功能需要openssl庫(在http://www.openssl.org/下載http://www.openssl.org/source/openssl-fips-2.0.9.tar.gz)
或者下載全包openssl-1.0.2o.tar

2.2Nginx安裝
安裝前確認linux下這些庫已經安裝
yum install perl
yum install gcc
yum install gcc-c++
yum -y install net-tools

1.將安裝包放到/home/nginx_install目錄下,截圖如下:

    技術分享圖片


2.安裝openssl-fips-2.0.9.tar.gz,執行命令如下
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。,安裝OpenSSL(http://www.openssl.org/source/)主要是為了讓tengine支持Https的訪問請求。具體是否安裝看需求。

cd /home/nginx_install
tar -zxvf openssl-1.0.2o.tar.gz
cd openssl-1.0.2o
#prefix配置安裝路徑
./config --prefix=/opt/ldkjdata/nginx/openssl-1.0.2o
make
make install

openssl:wget http://www.openssl.org/source/openssl-0.9.8m.tar.gz 解壓不make
最新版本為openssl-1.0.2o.tar



3.安裝zlib-1.2.8.tar.gz
Zlib是提供資料壓縮之用的函式庫,當Tengine想啟用GZIP壓縮的時候就需要使用到Zlib(http://www.zlib.net/)

cd /home/nginx_install
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/opt/ldkjdata/nginx/zlib-1.2.11
make
make install



4.安裝pcre-8.37.tar.gz
PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx rewrite依賴於PCRE庫,所以在安裝Tengine前一定要先安裝PCRE,最新版本的PCRE可在官網(http://www.pcre.org/)獲取。

cd /home/nginx_install
tar -zxvf pcre-8.42.tar.gz
cd pcre-8.42
./configure --prefix=/opt/ldkjdata/nginx/pcre-8.42
make
make install


5.安裝nginx-1.8.0.tar.gz

cd /home/nginx_install
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0

#with-pcre指定依賴包位置,prefix配置安裝路徑
./configure --with-pcre=../pcre-8.42 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-fips-2.0.16 --prefix=/opt/ldkjdata/nginx/nginx-1.14.0

或者
./configure --with-pcre=../pcre-8.42 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.2o --with-http_ssl_module --prefix=/opt/ldkjdata/nginx/nginx-1.14.0

make
make install


3Nginx檢測
cd /opt/ldkjdata/nginx/nginx-1.14.0/sbin
./nginx -t
看到如下提示,表示安裝成功。
nginx: the configuration file /opt/ldkjdata/nginx/nginx-1.14.0/conf/nginx.conf syntax is ok
nginx: configuration file /opt/ldkjdata/nginx/nginx-1.14.0/conf/nginx.conf test is successful


4.Nginx啟動和端口查看

cd /opt/ldkjdata/nginx/nginx-1.14.0/sbin
./nginx 啟動
./nginx -s stop 停止
./nginx -s reload 修改配置後重新加載配置
netstat -ntlp 查看tcp是否正常監聽


5.一個簡單的Nginx轉發例子
1.需求
短信服務器列表三臺,提供服務的地址如下:

http://192.168.88.21:8091/smsserver/services/sendSms?wsdl
http://192.168.88.22:8091/smsserver/services/sendSms?wsdl
http://192.168.88.23:8091/smsserver/services/sendSms?wsdl
彩信服務器列表三臺,提供服務的地址如下:

http://192.168.88.21:8092/msserver/services/sendMms?wsdl
http://192.168.88.22:8092/mmsserver/services/sendMms?wsdl
http://192.168.88.23:8092/mmsserver/services/sendMms?wsdl

Nginx安裝在另外一臺單獨機器上(公網IP為public_ip),對外提供服務地址如下:
http://public_ip:8090/smsserver/services/sendSms?wsdl 短信發送服務
http://public_ip:8090/mmsserver/services/sendMms?wsdl 彩信發送服務

2.對於以上需求,配置/opt/nginx-1.8.0/conf/nginx.conf如下:
#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;
}

http {
include mime.types;
default_type application/octet-stream;

#log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
# ‘$status $body_bytes_sent "$http_referer" ‘
# ‘"$http_user_agent" "$http_x_forwarded_for"‘;

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;
#設定請求緩沖, start
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#設定請求緩沖, end

#設定提供服務的服務器,start
#短信發送服務器
upstream smsserver{
#weigth 表示權重,權值越大,分配幾率越大
#max_fails 當有max_fails個請求失敗,就表示後端的服務器不可用,默認為1,將其設置為0可以關閉檢查
#fail_timeout 在以後的fail_timeout時間內nginx不會再把請求發往已檢查出標記為不可用的服務器
server 192.168.88.21:8091 weight=5 max_fails=5 fail_timeout=600s;
server 192.168.88.22:8091 weight=5 max_fails=5 fail_timeout=600s;
server 192.168.88.23:8091 weight=5 max_fails=5 fail_timeout=600s;
}
#彩信發送服務器
upstream mmsserver{
#weigth 表示權重,權值越大,分配幾率越大
#max_fails 當有max_fails個請求失敗,就表示後端的服務器不可用,默認為1,將其設置為0可以關閉檢查
#fail_timeout 在以後的fail_timeout時間內nginx不會再把請求發往已檢查出標記為不可用的服務器
server 192.168.88.21:8092 weight=5 max_fails=5 fail_timeout=600s;
server 192.168.88.22:8092 weight=5 max_fails=5 fail_timeout=600s;
server 192.168.88.23:8092 weight=5 max_fails=5 fail_timeout=600s;
}
#設定提供服務的服務器,end

#gzip on;

server {
listen 8090;
server_name localhost;
#設定請求轉發規則, start
#規則采用最長匹配,/smsserver/*優先匹配/smsserver,/mmsserver/*優先匹配/mmsserver,/aaaaaaaa/*因為沒有任何匹配,最後匹配到/
#規則一
location / {
proxy_pass http://localhost:80;
}
#規則二
location /smsserver {
proxy_pass http://smsserver;
}
#規則三
location /mmsserver {
proxy_pass http://mmsserver;
}
#設定請求轉發規則, end
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 80;
server_name localhost;
#設定請求轉發規則, start
location / {
#定義服務器的默認網站根目錄位置
root /home/work/statichtml/index.html;
#定義首頁索引文件的名稱
#index index.php index.html index.htm;
#請求轉向orderServer定義的服務器列表
# proxy_pass http://server;

#以下是一些反向代理的配置可刪除.
# proxy_redirect off;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#允許客戶端請求的最大單文件字節數
# client_max_body_size 10m;
#緩沖區代理緩沖用戶端請求的最大字節數,
# client_body_buffer_size 128k;
#nginx跟後端服務器連接超時時間(代理連接超時)
# proxy_connect_timeout 90;
#後端服務器數據回傳時間(代理發送超時)
# proxy_send_timeout 90;
#連接成功後,後端服務器響應時間(代理接收超時)
# proxy_read_timeout 90;
#設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
# proxy_buffer_size 4k;
#proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置
# proxy_buffers 4 32k;
#高負荷下緩沖大小(proxy_buffers*2)
# proxy_busy_buffers_size 64k;
#設定緩存文件夾大小,大於這個值,將從upstream服務器傳
# proxy_temp_file_write_size 64k;
}
#設定請求轉發規則, end
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}


6.例子2(暴露接口,但隱藏web項目的根目錄)
上面6中例子有個問題是,直接將我整個web根目錄smsserver和mmsserver暴露到公網了,其實我的目的只是想暴露兩個接口地址,所以6有待優化,舉個例子如下:
1.需求
接口服務器列表兩臺臺,提供服務的地址如下:
接口一:
http://192.168.88.21:8082/myweb/interface/getData
http://192.168.88.22:8082/myweb/interface/getData
接口二:
http://192.168.88.21:8082/myweb/interface/sendData
http://192.168.88.22:8082/myweb/interface/sendData

Nginx安裝在另外一臺單獨機器上(公網IP為public_ip),對外提供服務地址如下:
http://public_ip:8081/myweb/interface/getData 接口一
http://public_ip:8081/myweb/interface/sendData 接口二
2.對於以上需求,配置/opt/nginx-1.8.0/conf/nginx.conf如下:

#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;
}
http {
include mime.types;
default_type application/octet-stream;

#log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
# ‘$status $body_bytes_sent "$http_referer" ‘
# ‘"$http_user_agent" "$http_x_forwarded_for"‘;

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;
#設定請求緩沖, start
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#設定請求緩沖, end

#設定提供服務的服務器,start
#我的接口服務器,兩臺
upstream myweb{
#weigth 表示權重,權值越大,分配幾率越大
#max_fails 當有max_fails個請求失敗,就表示後端的服務器不可用,默認為1,將其設置為0可以關閉檢查
#fail_timeout 在以後的fail_timeout時間內nginx不會再把請求發往已檢查出標記為不可用的服務器
server 192.168.88.23:8082 weight=5 max_fails=5 fail_timeout=600s;
server 192.168.88.24:8082 weight=5 max_fails=5 fail_timeout=600s;
}
#設定提供服務的服務器,end

#gzip on;

server {
listen 80;
server_name localhost;
#設定請求轉發規則, start
#規則采用最長匹配,即長度最長優先匹配,最後不匹配的走/進行匹配
#規則一
location / {
root html;
index index.html index.htm;
}
#設定請求轉發規則, end
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 8081;
server_name localhost;
#設定請求轉發規則, start
#規則采用最長匹配,即長度最長優先匹配,最後不匹配的走/進行匹配
#規則一
location / {
root html;
index index.html index.htm;
}
#規則二,查詢接口一
location /myweb/interface/getData {
proxy_pass http://myweb;
#轉發請求的原IP地址,程序中通過request.getHeader("Proxy-Client-IP")獲得ip
proxy_set_header Host $host;
#如果是有涉及redirect的服務,一定要加上端口8081,否則默認tomcat在redirect時候默認找80端口
#proxy_set_header Host $host:8081;
proxy_set_header Proxy-Client-IP $remote_addr;
}
#規則三,查詢接口二
location /myweb/interface/sendData {
proxy_pass http://myweb;
#轉發請求的原IP地址,程序中通過request.getHeader("Proxy-Client-IP")獲得ip
proxy_set_header Host $host;
#如果是有涉及redirect的服務,一定要加上端口8081,否則默認tomcat在redirect時候默認找80端口
#proxy_set_header Host $host:8081;
proxy_set_header Proxy-Client-IP $remote_addr;
}
#設定請求轉發規則, end
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

7.例子3 文件映射
/conf/nginx.conf中第一行的運行用戶改為
user root;
location /uploadfile {
root /opt/staticdata/;
access_log /opt/nginx/logs/upload_access.log;
proxy_store_access user:rw group:rw all:rw;
}

當訪問http://ip:port/uploadfile/aa/bb/cc.jpg
相當於訪問 /opt/staticdata/uploadfile/aa/bb/cc.jpg

8.說明
以上安裝教程經過親自實踐並優化,主要參考https://www.cnblogs.com/IPYQ/p/6609969.html

linux centos7 nginx 安裝部署和配置