1. 程式人生 > >CentOS7 Nginx安裝及配置反向代理

CentOS7 Nginx安裝及配置反向代理

ges xxx 網絡 update bad blank 模塊 sendfile 找到

背景:

  Mono (Mono JIT compiler version 5.4.0.201 )

  jexus-5.8.2-x64(《CentOS7 安裝 jexus-5.8.2-x64》)

  VirtualBox5.1.22(3個CentOS7系統) (192.168.5.147、192.168.5.182、192.168.5.183)

  參考資料:

  http://www.cnblogs.com/guogangj/p/4131704.html(HappyAA服務器部署筆記1(nginx+tomcat的安裝與配置))

  http://www.cnblogs.com/guogangj/p/5207104.html(簡易nginx TCP反向代理設置)

  http://www.cnblogs.com/bass6/p/5948199.html(CNginx反向代理設置 從80端口轉向其他端口)

  http://www.cnblogs.com/jeffzhang/p/4664457.html(Centos 7 上使用nginx為Node.js配置反向代理時錯誤:(13: Permission denied) while connecting to upstream)

  http://www.cnblogs.com/mfrbuaa/p/4866135.html(解決Nginx的connect() to 127.0.0.1:8080 failed (13: Permission denied) while connect)

  http://www.cnblogs.com/zrbfree/p/6419043.html(nginx 安裝時候報錯:make: *** No rule to make target `build‘, needed by `default‘. Stop.)

  寫這篇文章也是為了記錄我的履試不爽的過程,怕以後很久不用就忘了,感謝園子及貢獻者。

1、三個CentOS7系統準備

在147機子基礎上完整復制了182及183,復制好後一樣要刷新一下網絡的MAC地址。

  技術分享

2、安裝Jexus《CentOS7 安裝 jexus-5.8.2-x64》本想只安裝182:8888,183:7777,index.html內容設置不同,但因設置好後始終把錯誤:"502 Bad Gateway"

於是將147:8080也安裝 上並設置

3、安裝Nginx

#yum update
更新一些庫和必要的支持,完了之後去下載一個nginx的最新版,如今我責編的版本是1.7.7:
#wget http://nginx.org/download/nginx-1.13.6.tar.gz
解壓縮
#tar -zvxf nginx-1.13.6.tar.gz
#cd nginx-1.13.6
nginx有很多很多編譯配置項,但由於我這是第一篇筆記,所以我基本上都使用了默認的配置:
#./configure --with-http_ssl_module --with-http_gzip_static_module
我只加了兩個選項,--with-http_ssl_module表示使用ssl模塊,--with-http_gzip_static_module表示使用gzip模塊,其它更詳細的配置就要參考nginx的文檔了:http://nginx.org/en/docs/configure.html

如果沒configure成功(會顯示XXX not found),那是因為有些依賴沒有被正確安裝.那麽先安裝一下這些依賴條件,通常是pcre,zlib這些,這麽一下就基本上可以了:
#yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

#make
#make install

可執行文件就會被安裝在: /usr/sbin/nginx (默認配置)

nginx基本使用

啟動nginx:
#cd /usr/local/nginx/sbin/
#./nginx

如果運行的時候不帶-c參數,那就采用默認的配置文件,即/etc/nginx/nginx.conf

查看運行進程狀態:
# ps aux | grep nginx

打開瀏覽器,訪問http://localhost/看看nginx的默認頁面:

技術分享

停止nginx:
#./nginx -s stop

重啟nginx(配置文件變動後需要重啟才能生效):
#./nginx -s reload

檢查配置文件是否正確:
#./nginx -t

查看nginx的pid:
cat /usr/local/nginx/logs/nginx.pid

查看nginx版本
$ ./nginx -v

回頭看編譯配置
# ./nginx -V

4、Nginx配置

#vi /etc/nginx/nginx.conf

user nginx;

worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}

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

  sendfile on;

  keepalive_timeout 65;

  server {
    listen 80;

    server_name localhost;

    location / {
      proxy_pass http://192.168.5.147:8080;
    }

  }
}

按上面這樣配置按理應該可以訪問http://192.168.5.147 顯示的應該是147:8080的網頁內容,但 就是報錯了

於是,這才查看日誌

#vi /var/log/nginx/error.log

2017/11/03 05:23:53 [crit] 1331#1331: *12 connect() to 192.168.5.147:8080 failed
(13: Permission denied) while connecting to upstream, client: 192.168.5.65, server: localhost,
request: "GET / HTTP/1.1", upstream: "http://192.168.5.147:8080/", host: "192.168.5.147"

園子中搜“(13: Permission denied) while connecting to upstream, client:”就找到原因,

type=AVC msg=audit(1509701033.988:119): avc: denied { name_connect } for pid=1331
comm="nginx" dest=8080 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_cache_port_t:s0 tclass=tcp_socket

處理:

#setsebool -P httpd_can_network_connect 1

到這裏應該就正常了,反向代理的後面設置。。。待補充

CentOS7 Nginx安裝及配置反向代理