背景:

  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)

  参考资料:

(​​HappyAA服务器部署笔记1(nginx+tomcat的安装与配置​​))

(​​简易nginx TCP反向代理设置​​)

(​​CNginx反向代理设置 从80端口转向其他端口​​)

(​​Centos 7 上使用nginx为Node.js配置反向代理时错误:(13: Permission denied) while connecting to upstream​​)

(​​解决Nginx的connect() to 127.0.0.1:8080 failed (13: Permission denied) while connect​​)

(​​nginx 安装时候报错:make: *** No rule to make target `build', needed by `default'. Stop.​​)

  写这篇文章也是为了记录我的履试不爽的过程,怕以后很久不用就忘了,感谢园子及贡献者。

1、三个CentOS7系统准备

在147机子基础上完整复制了182及183,复制好后一样要刷新一下网络的MAC地址。

  

CentOS7 Nginx安装及配置反向代理_centos

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


make: *** 没有规则可以创建“default”需要的目标“build” 问题的解决 引用: yum install pcre-devel yum install zlib zlib-devel yum install openssl openssl-devel //也可用一条命令代替 yum install pcre-devel zlib zlib-devel openssl openssl-devel


 

常见问题:


nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 80端口占用


View Code


 

 

可执行文件就会被安装在: /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的默认页面:

CentOS7 Nginx安装及配置反向代理_javascript_02

停止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

到这里应该就正常了,反向代理的后面设置

下面是设置两个服务器

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 错误​​