设为首页 收藏本站
查看: 853|回复: 0

[经验分享] 基于Centos7.2的nginx部署

[复制链接]

尚未签到

发表于 2018-4-22 11:29:02 | 显示全部楼层 |阅读模式
基于Centos7.2的nginx部署

  

  部署背景:使用Nginx作为Tomcat的负载平衡器。
  部署步骤:

  •   安装zlib-devel、pcre-devel等依赖包
    DSC0000.png

    [root@nginx ~]#  yum install -y gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel

      注:结合proxy和upstream模块实现后端web负载均衡
      结合nginx默认自带的ngx_http_proxy_module模块 和ngx_http_upstream_module模块实现后端服务器的健康检查。
      Proxy:实现反向代理
      Upstream:实现负载均衡
      Nginx在使用HTTPS服务时要用到openssl-devel模块,如果不安装openssl相关包,安装Nginx的过程会报错。
  •   创建nginx用户
    [root@nginx ~]# useradd -s /sbin/nologin www

    [root@nginx ~]# grep www /etc/passwd  ##查看nginx用户www是否建立

      www:x:1000:1000::/home/www:/sbin/nologin
  •   编译安装nginx
    [root@nginx src]# tar -zxvf nginx-1.13.0.tar.gz

    [root@nginx src]# cd nginx-1.13.0

    [root@nginx nginx-1.13.0]# ./configure --prefix=/usr/local/nginx1.10 --user=www  --group=www --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_flv_module && make && make install

      其中:--prefix=/usr/local/nginx1.10表示nginx包安装路径
  •   创建nginx软连接,方便nginx程序的执行
    [root@nginx nginx-1.13.0]# ln -s /usr/local/nginx1.10/sbin/nginx /usr/local/sbin/

  •   nginx语法检查
    [root@nginx nginx-1.13.0]# nginx -t

    DSC0001.png

  •   编写nginx服务脚本
    [root@nginx ~]# vim /etc/init.d/nginx

      #!/bin/sh
      #
      # nginx - this script starts and stops the nginx daemon
      #
      # chkconfig:   - 85 15
      # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
      #               proxy and IMAP/POP3 proxy server
      # processname: nginx
      # config:      /usr/local/nginx1.10/conf/nginx.conf
      # pidfile:     /usr/local/nginx1.10/logs/nginx.pid
      

      nginxd=/usr/local/nginx1.10/sbin/nginx
      nginx_config=/usr/local/nginx1.10/conf/nginx.conf
      nginx_pid=/usr/local/nginx1.10/logs/nginx.pid
      RETVAL=0
      prog="nginx"
      

      

      # Source function library.
      . /etc/rc.d/init.d/functions
      # Source networking configuration.
      . /etc/sysconfig/network
      

      # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0

      

      nginx="/usr/local/sbin/nginx"
      prog=$(basename $nginx)
      

      NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
      

      lockfile=/var/lock/subsys/nginx
      

      start() {
    [ -x $nginx ] || exit 5

    [ -f $NGINX_CONF_FILE ] || exit 6

      echo -n $"Starting $prog: "
      daemon $nginx -c $NGINX_CONF_FILE
      retval=$?
      echo
    [ $retval -eq 0 ] && touch $lockfile

      return $retval
      }
      

      stop() {
      echo -n $"Stopping $prog: "
      killproc $prog -QUIT
      retval=$?
      echo
    [ $retval -eq 0 ] && rm -f $lockfile

      return $retval
      }
      

      restart() {
      configtest || return $?
      stop
      start
      }
      

      reload() {
      configtest || return $?
      echo -n $"Reloading $prog: "
      killproc $nginx -HUP
      RETVAL=$?
      echo
      }
      

      force_reload() {
      restart
      }
      

      configtest() {
      $nginx -t -c $NGINX_CONF_FILE
      }
      

      rh_status() {
      status $prog
      }
      

      rh_status_q() {
      rh_status >/dev/null 2>&1
      }
      case "$1" in
      start)
      rh_status_q && exit 0
      $1
      ;;
      stop)
      rh_status_q || exit 0
      $1
      ;;
      restart|configtest)
      $1
      ;;
      reload)
      rh_status_q || exit 7
      $1
      ;;
      force-reload)
      force_reload
      ;;
      status)
      rh_status
      ;;
      condrestart|try-restart)
      rh_status_q || exit 0
      ;;
      *)
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
      exit 2
      esac
  •   添加开机自启动服务
    [root@nginx ~]# chmod +x /etc/init.d/nginx

    [root@nginx ~]# chkconfig --add nginx

    [root@nginx ~]# chkconfig nginx on

    [root@nginx ~]# chkconfig --list |grep nginx

      nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off
  •   启动nginx服务
    [root@nginx ~]# /usr/local/sbin/nginx start

      nginx: invalid option: "start"
    [root@nginx ~]# /etc/init.d/nginx start

      Starting nginx (via systemctl):  Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
    [FAILED]

    DSC0002.png

      以上我们可以看出,nginx启动失败!以下是解决方法:
    [root@nginx ~]# /usr/local/sbin/nginx

    [root@nginx ~]# /etc/init.d/nginx start

      Starting nginx (via systemctl):                            [  OK  ]
    DSC0003.png

    DSC0004.png

  •   配置nginx反向代理:作用是(反向代理+负载均衡+健康探测)
      修改nginx主配置文件:
    [root@nginx ~]# vim /usr/local/nginx1.10/conf/nginx.conf

      

      user  www www;
      worker_processes    2;
      worker_cpu_affinity 0101 1010;
      error_log  logs/error.log;
      #error_log  logs/error.log  notice;
      #error_log  logs/error.log  info;
      worker_rlimit_nofile 10240;
      pid        logs/nginx.pid;
      events{
      use epoll;
      worker_connections  4096;
      }
      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;
      server_tokens off;
      sendfile        on;
      tcp_nopush     on;
      #keepalive_timeout  0;
      keepalive_timeout  65;
      #Compression Settings
      gzip on;
      gzip_comp_level 6;
      gzip_http_version 1.1;
      gzip_proxied any;
      gzip_min_length 1k;
      gzip_buffers 16 8k;
      gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascriptapplication/xml;
      gzip_vary on;
      #end gzip
      # http_proxy Settings
      client_max_body_size   10m;
      client_body_buffer_size   128k;
      proxy_connect_timeout   75;
      proxy_send_timeout   75;
      proxy_read_timeout   75;
      proxy_buffer_size   4k;
      proxy_buffers   4 32k;
      proxy_busy_buffers_size   64k;
      proxy_temp_file_write_size  64k;
      #load balance Settings
      upstream backend_tomcat {
      server 192.168.100.126:8080 weight=1 max_fails=2 fail_timeout=10s;   ##需要更改为tomcat的ip
      server 192.168.100.127:8080 weight=1 max_fails=2 fail_timeout=10s;    ##需要更改为tomcat的ip
      }
      #virtual host Settings
      server{
      listen       80;
      server_name  www.benet.com;
      charset utf-8;
      location / {
      root html;
      index  index.jsp index.html index.htm;
      }
      location ~* \.(jsp|do)$ {
      proxy_pass  http://backend_tomcat;
      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;
      proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
      }
      location /nginx_status {
      stub_status on;
      access_log off;
      allow 192.168.100.0/24;    ##需要更改tomcat的ip段
      deny all;
      }
      }
      }
  •   重启使其生效
    [root@nginx conf]# /usr/local/sbin/nginx

    [root@nginx conf]# service nginx restart

      Restarting nginx (via systemctl):                          [  OK  ]
  • [root@nginx ~]# firewall-cmd --permanent --add-port=80/tcp

      success
    [root@nginx ~]# firewall-cmd --reload

      success
    DSC0005.png

      

  以上就是nginx部署的基本步骤!
  

  扩展:
  除了nginx语法检查nginx安装和浏览是否正常外,还有两种常用的方法:
  1.使用wget命令检查

[root@nginx ~]# wget 127.0.0.1

DSC0006.png

  2.使用curl命令检查
[root@nginx ~]# curl 127.0.0.1

DSC0007.png

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-450375-1-1.html 上篇帖子: CentOS 6.9编译安装新版本内核 下篇帖子: 基于Centos6的tomcat部署
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表