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

[经验分享] nginx-10939891

[复制链接]

尚未签到

发表于 2018-11-12 07:15:31 | 显示全部楼层 |阅读模式
  网站服务  代理服务   搭建Nginx服务器
  nginx-1.8.0.tar.gz
  rpm  -q  gcc   gcc-c++
  yum  -y groupinstall  "开发工具"
  yum  -y  install  pcre-devel     openssl-devel
  rpm  -q httpd  &&  service  httpd  stop
  chkconfig    httpd  off
  useradd  -s  /sbin/nologin    nginx
  ./configure    --prefix=/usr/local/nginx  --user=nginx  --group=nginx   --with-http_ssl_module
  make
  make  install
  ls  /usr/local/nginx/
  conf     配置文件
  nginx.conf  主配置文件
  nginx.conf.default  模版文件
  html    网页目录
  logs     日志文件
  sbin     命令
  nginx
  启动nginx 进程
  /usr/local/nginx   -h
  常用选项
  -v:查看nginx版本
  -V:查看编译参数
  -t:测试默认配置文件
  -c:指定配置文件
  [root@A nginx]# ./sbin/nginx
  [root@A nginx]# netstat -utnlap | grep :80
  tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7223/nginx
  [root@A nginx]#
  [root@A nginx]# ls logs/
  access.log  error.log  nginx.pid
  [root@A nginx]#
  echo  123    >  /usr/local/nginx/html/a.html
  yum  -y  install elinks
  elinks  --dump  http://localhost/a.html
  123
  Nginx进程管理
  杀死nginx进程的方式
  /usr/local/nginx/sbin/nginx -s stop
  pkill   -9   nginx
  killall  -9  nginx
  kill   -信号  nginx
  TERM, INT 快速关闭 QUIT 从容关闭,关闭主进程及子进程HUP  重载配置文件
  USR1 重新打开日志文件 USR2 平滑升级可执行程序
  [root@A nginx]# /usr/local/nginx/sbin/nginx -s stop
  [root@A nginx]# elinks  --dump http://localhost/a.html
  ELinks: 拒绝连接
  [root@A nginx]#
  [root@A nginx]# /usr/local/nginx/sbin/nginx
  [root@A nginx]# elinks  --dump http://localhost/a.html
  123
  [root@A nginx]#
  kill  -HUP  `/usr/local/nginx/logs/nginx.pid`
  elinks  --dump http://localhost:8080/a.html
  平滑升级nginx
  22  tar -zxvf nginx-1.9.2.tar.gz
  cd nginx-1.9.2
  /usr/local/nginx/sbin/nginx -V
  ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
  make
  mv  /usr/local/nginx/sbin/nginx   /usr/local/nginx/sbin/nginxold
  mv objs/nginx  /usr/local/nginx/sbin/
  /usr/local/nginx/sbin/nginx -v
  [root@A nginx-1.9.2]# make  upgrade
  /usr/local/nginx/sbin/nginx -t
  nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
  sleep 1
  test -f /usr/local/nginx/logs/nginx.pid.oldbin
  kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
  [root@A nginx-1.9.2]#
  主配置文件的语法格式
  http{
  server {
  listen       80;
  server_name  localhost;
  location / {  
  root   html;
  index  index.html   index.htm;
  }
  }
  }
  ----------------------------------------------------------------
  配置Nginx虚拟主机  server  {   }
  基于端口的虚拟主机(通过端口区分客户端的访问)
  端口          网页目录
  http://192.168.1.254:80          /usr/local/nginx/html
  http://192.168.1.254:8080     /wwwdir
  vim   nginx.conf
  http {
  server   {
  listen   80;
  location   /  {
  root    html;
  index   a.html;
  }
  }
  server {
  listen   8080;
  location  / {
  root   /wwwdir;
  index  index.html;
  }
  }
  }
  -------------------------------------------------------------
  基于域名的虚拟主机(通过主机名区分客户端的访问)
  http://www.tarena.com   /usr/local/nginx/html
  http://bbs.tarena.com      /bbsdir
  192.168.1.254
  vim   nginxsername.conf
  http{
  server {
  listen  80;
  server_name   www.tarena.com;
  location  /  {
  root   html;
  index  index.html;
  }
  }
  server {
  listen  80;
  server_name   bbs.tarena.com;
  location  /  {
  root   /bbsdir;
  index  index.html;
  }
  }
  }
  :wq
  mkdir   /bbsdir
  echo  123   >  /bbsdir/index.html
  echo  567  > /usr/local/nginx/html/index.html
  客户端 要能够解析主机名到web服务器的Ip地址
  vim  /etc/hosts
  192.168.1.254  www.tarena.com  www
  192.168.1.254  bbs.tarena.com  bbs
  :wq
  [root@A conf]# elinks  --dump http://bbs.tarena.com
  123
  [root@A conf]# elinks  --dump http://www.tarena.com
  567
  [root@A conf]#
  -----------------------------------------------------------------
  基于Ip地址的虚拟主机(通过ip地址区分客户端的访问)
  http://1.0.0.254                      /usr/local/nginx/html/
  http://192.168.1.254              /bbsdir/
  cat  ip.conf
  http {
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  server  {
  listen  192.168.1.254:80;
  location / {
  root  /bbsdir;
  index index.html;
  }
  }
  server {
  listen      1.0.0.254:80;
  location / {
  root   html;
  index  index.html index.htm;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
  root   html;
  }
  }
  :wq
  客户访问
  ping  192.168.1.254
  ping  1.0.0.254
  [root@A ~]# elinks  --dump  http://1.0.0.254
  567
  [root@A ~]# elinks  --dump  http://192.168.1.254
  123
  [root@A ~]#
  -----------------------------------------------------------------
  客户端访问控制   location  /  {       }
  默认允许所有客户端访问网站目录下网页文件
  location / {
  .....
  .....
  allow  192.168.1.188;
  deny   all;
  }
  用户验证 (访问网页文件时,需要提交用户名和 密码)
  location   /  {
  .....
  .....
  allow  192.168.1.188;
  deny   all;
  auth_basic  "auth-domain";
  auth_basic_user_file     "/usr/local/nginx/conf/user.txt";}
  rpm  -q   httpd-tools
  [root@A conf]# htpasswd -c /usr/local/nginx/conf/user.txt   webadmin
  New password:
  Re-type new password:
  Adding password for user webadmin
  [root@A conf]# cat /usr/local/nginx/conf/user.txt
  webadmin:.GHgOK5P5MaiY
  [root@A conf]#
  -----------------------------------------------------------------
  配置虚拟主机
  http://192.168.1.254              /usr/local/nginx/html/
  http://192.168.1.254:8090    /bbsdir/
  但只允许从地址192.168.1.188客户端访问 网站服务器192.168.1.254的8090端口 并要提交正确的用户名和密码才可以访问
  ---------------------------------------------------------------
  配置Nginx反向代理
  vim  nginx.conf
  upstream  "webgrp" {
  server   192.168.1.1:80 weight=3;
  server   192.168.1.2:80;
  }
  server {
  listen  80;
  location / {
  #proxy_pass  http://192.168.1.1;
  proxy_pass   http://webgrp;
  }
  }
  :wq
  nginx目前支持4种分配方式轮询(默认的): 逐一循环调度Weight:指定轮询几率,权重值和访问比率成正比
  ip_hash:根据客户端IP分配固定的后端服务器Fair:按后端服务器响应时间短的优先分配
  upstream   sergrp  {
  #ip_hash;
  #server 192.168.8.5:80   weight=2;
  server 192.168.8.5:80   down;
  server 192.168.8.4:8080;
  server 192.168.8.6:80   backup;
  server 192.168.8.3:80   max_fails=2   fail_timeout=30;
  }


运维网声明 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-633850-1-1.html 上篇帖子: Nginx-8438262 下篇帖子: nginx-xbzy
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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