chenjiali 发表于 2017-12-22 09:13:22

CentOS7安装Nginx并部署

  服务器IP是192.168.36.136
  1、直接yum install nginx即可
  2、主配置文件是/etc/nginx/下的nginx.conf,另外一个是/etc/nginx/conf.d/下的default.conf
  主配置文件最末行通过 include /etc/nginx/conf.d/*.conf;引入
  3、service nginx start启动后,访问http://192.168.36.136/会出现Nginx的默认首页
  默认首页配置要看default.conf里面的server
  listen 80;#监听端口,如果换成81,那么访问就是http://192.168.36.136:81/
  server_namelocalhost;#监听地址,nginx服务器地址
  #下面就是根据location路由规则找到默认页面的,如果index.html不存在会找index.htm;对于详细如有规则可参考Nginx Location配置总结
  location / {
  root   /usr/share/nginx/html;
  indexindex.html index.htm;
  }

  4、修改了配置文件后可以通过nginx -t命令检查一下,nginx -s>  5、现在在192.168.36.134服务器上已经启动了一个tomcat,并且外部测试可以访问http://192.168.36.134:8080/,现在就想通过nginx访问
  为了保持之前的location,又添加一个如下
  location /test/ {
  proxy_pass http://192.168.36.134:8080/index.jsp;
  }

  配置好后,nginx -s>
  这个错误页面就是
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
  root   /usr/share/nginx/html;
  }
  配置下指向的页面,也就是说,路由配置起作用了,只是nginx后台与apache服务器之间的问题,百度了centos7 502问题,这是红帽和centos6.6版出现的问题,解决方案如下
  yum -y install policycoreutils-python
  cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
  semodule -i mynginx.pp
  6、再次访问http://192.168.36.136/test/页面显示如下

  页面显示这样,查看下面报错,没有引入图片和CSS静态文件,这种错就是配置的时候根路径是/test/,后面真正用的时候就直接写项目根路径即可
  7、上面只是用nginx配置了一台服务器,要配置多台实现负载均衡效果配置如下
  在http下添加upstream(文件/etc/nginx/nginx.conf)
  upstream hostname {
  server 192.168.36.136:8080 weight=1;
  server 192.168.36.134:8080 weight=10;
  }
  然后修改server下路由规则为/test/的location
  location /test/ {
  proxy_pass http://hostname/index.jsp;
  }
  如此后重新加载配置文件访问http://192.168.36.136/test/
页: [1]
查看完整版本: CentOS7安装Nginx并部署