CentOS7 Nginx安裝及配置反向代理

背景:html

  Mono (Mono JIT compiler version 5.4.0.201 )nginx

  jexus-5.8.2-x64(《CentOS7 安裝 jexus-5.8.2-x64》瀏覽器

  VirtualBox5.1.22(3個CentOS7系統) (192.168.5.14七、192.168.5.18二、192.168.5.183)tomcat

  參考資料:服務器

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

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

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

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

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

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

  寫這篇文章也是爲了記錄個人履試不爽的過程,怕之後好久不用就忘了,感謝園子及貢獻者。

一、三個CentOS7系統準備

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

  

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

因而將147:8080也安裝 上並設置

三、安裝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基本使用

程序位置:/usr/local/nginx/sbin/nginx 

配置文件位置:/usr/local/nginx/conf/nginx.conf

 

啓動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

四、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

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

下面是設置兩個服務器


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;

  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 /var/log/nginx/access.log main;

  sendfile on;
  #tcp_nopush on;

  keepalive_timeout 65;

  #gzip on;

  upstream test {
    server 192.168.5.182:8888;
    server 192.168.5.183:7777;
  }
  server {
    listen 80;
    server_name localhost;

    location / {
      proxy_pass http://test;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Cookie $http_cookie;
    }
  }
}

 

nginx集羣報錯「upstream」directive is not allow here 錯誤

相關文章
相關標籤/搜索