hq8501 发表于 2018-11-16 08:37:18

Nginx安装与优化

  Nginx安装
  1、下载依赖包
  yum install pcre pcre-devel -y
  yum install openssl-devel –y
  2、建立nginx用户
  useradd nginx -s /sbin/nologin –M
  3、创建app目录
  mkdir /application
  4、创建下载点目录
  mkdir /home/oldboy/tools –p
  5、切换目录并下载
  cd /home/oldboy/tools/
  wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
  6、解压
  tar xf nginx-1.6.3.tar.gz
  7.编译安装nginx
  cd nginx-1.6.3
  ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
  8、使其生效
  make
  make install
  9、创建软链接
  ln -s /application/nginx-1.6.3/ /application/nginx
  10、启动nginx
  /application/nginx/sbin/nginx
  11、检查有没有nginx进程
  ps -ef |grep nginx|grep -v grep
  配置站点
  # ls -l|grep -v temp
  total 36
  drwxr-xr-x 2 rootroot 4096 May 11 16:02 conf    #配置文件目录
  drwxr-xr-x 2 rootroot 4096 May 11 16:02 html    #站点(默认网站目录)
  drwxr-xr-x 2 rootroot 4096 May 11 16:13 logs    #错误、访问日志、
  drwxr-xr-x 2 rootroot 4096 May 11 16:02 sbin    #启动命令
  cd /html
  vim index.html
  
  oldboy,s Nginx server blog.
  
  Hi, I am oldboy. My blog address is
  http://oldboy.blog.51cto.com
  
  
  基于域名的虚拟主机配置
  一:命令历史记录
  切换到配置文件目录
  cd conf/
  查看包含目录
  ls -l nginx.conf
  操作前备份
  cp nginx.conf{,.ori}
  去掉配置文件中无用东西
  egrep -v "#|^$" nginx.conf.default >nginx.conf
  编辑配置文件
  vim nginx.conf
  操作完检查
  cat nginx.conf
  创建站点目录
  mkdir ../html/{www,bbs} –p
  操作完检查
  tree ../html/
  追加一些内容到站点目录
  echo "www.etiantian.org" > ../html/www/index.html
  echo "bbs.etiantian.org" > ../html/bbs/index.html
  操作完检查
  cat ../html/{www,bbs}/index.html
  检查语法
  /application/nginx/sbin/nginx –t
  优雅重启

  /application/nginx/sbin/nginx -s>  编辑本地hosts文件
  vim /etc/hosts
  ping看是否返回本机IP
  ping bbs.etiantian.org
  curl访问是否成功
  curl www.etiantian.org
  curl bbs.etiantian.org
  二:具体演示
  # cat nginx.conf
  worker_processes1;
  events {
  worker_connections1024;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  sendfile      on;
  keepalive_timeout65;
  server {
  listen       80;
  server_namewww.etiantian.org;
  location / {
  root   html/www;
  indexindex.html index.htm;
  }
  }
  server {
  listen       80;
  server_namebbs.etiantian.org;
  location / {
  root   html/bbs;
  indexindex.html index.htm;
  }
  }
  }
  创建站点目录
  # mkdir ../html/{www,bbs} -p
  # tree ../html/
  ../html/
  ├── 50x.html
  ├── bbs
  ├── index.html
  └── www
  2 directories, 2 fil
  # echo "www.etiantian.org" > ../html/www/index.html
  # echo "bbs.etiantian.org" > ../html/bbs/index.html
  # cat ../html/{www,bbs}/index.html
  www.etiantian.org
  bbs.etiantian.org
  检查语法
  # /application/nginx/sbin/nginx -t
  nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  重新加载

  # /application/nginx/sbin/nginx -s>  增加域名解析
  # vim /etc/hosts
  10.0.0.7 www.etiantian.org bbs.etiantian.org
  ping检查返回值是否是对应IP
  # ping www.etiantian.org
  PING www.etiantian.org (10.0.0.7) 56(84) bytes of data.
  64 bytes from www.etiantian.org (10.0.0.7): icmp_seq=1 ttl=64 time=0.020 ms
  ################
  # ping bbs.etiantian.org
  访问
  # curl www.etiantian.org
  www.etiantian.org
  # curl bbs.etiantian.org
  bbs.etiantian.org
  到此,基于域名的虚拟主机就配好了
  ---------------------------------------
  经验总结:
  # cat /etc/hosts
  127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  10.0.0.7 www.etiantian.org bbs.etiantian.org
  在工作中只需在DNS购买的管理界面里将域名解析成A记录 ,将两域名以A记录的形式解析到公网IP。
  windows中:WIN+R输入drivers编辑\etc\hosts文件 把解析写进去
  基于域名的虚拟主机访问原理
  HTTP访问原理
  浏览器输入www.etiantian.org。
  在请求报文的请求头会有一行字段。
  表示客户端告诉服务器,想要这个主机的内容
  内容到达服务器后
  服务器阅读请求报文,就会返回对应的响应报文。
  如果没有带host请求头,默认会返回配置文件中第一个域名
  =============================================================
  实战:增加blog.etiantian.org ============>html/blog,浏览域名显示blog.etiantian.org
  a、增加默认配置/application/nginx/conf
  server {
  listen       80;
  server_nameblog.etiantian.org;
  location / {
  root   html/blog;
  indexindex.html index.htm;
  }
  }
  b、创建站点目录
  # mkdir ../html/blog -p
  # tree ../html/
  ../html/
  ├── 50x.html
  ├── bbs
  │?? └── index.html
  ├── blog
  ├── index.html
  └── www
  └── index.html
  c、首页
  # echo "blog.etiantian.org" > ../html/blog/index.html
  # cat ../html/{www,bbs,blog}/index.html
  www.etiantian.org
  bbs.etiantian.org
  blog.etiantian.org
  d、检查配置文件语法
  # /application/nginx/sbin/nginx -t
  e、修改了配置文件,需要优雅重启

  /application/nginx/sbin/nginx -s>  f、域名解析
  # vim /etc/hosts
  10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org
  g、检查返回是否是自己主机IP
  # ping blog.etiantian.org
  PING www.etiantian.org (10.0.0.7) 56(84) bytes of data.
  64 bytes from www.etiantian.org (10.0.0.7): icmp_seq=1 ttl=64 time=0.028 ms
  h、进行访问
  # curl blog.etiantian.org
  blog.etiantian.org
  i、windows域名解析
  将10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org放进C:\Windows\System32\drivers\etc\hosts文件中
  j、浏览器进行访问
  浏览器访问成功,并显示:blog.etiantian.org
  ========================================
  基于端口的Nginx
  a、修改默认配置
  # cat nginx.conf
  worker_processes1;
  events {
  worker_connections1024;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  sendfile      on;
  keepalive_timeout65;
  server {
  listen       8001;
  server_namewww.etiantian.org;
  location / {
  root   html/www;
  indexindex.html index.htm;
  }
  }
  server {
  listen       8002;
  server_namewww.etiantian.org;
  location / {
  root   html/bbs;
  indexindex.html index.htm;
  }
  }
  server {
  listen       8003;
  server_namewww.etiantian.org;
  location / {
  root   html/blog;
  indexindex.html index.htm;
  }
  }
  }
  b、检查语法
  # /application/nginx/sbin/nginx -t
  c、优雅重启

  # ../sbin/nginx -s>  d、进行访问
  # curl http://www.etiantian.org:8001
  www.etiantian.org
  # curl http://www.etiantian.org:8002
  bbs.etiantian.org
  # curl http://www.etiantian.org:8003
  blog.etiantian.org
  =======================================================
  基于IP的虚拟主机很少用到就不做赘述
  Nginx优化
  Nginx常用选项:
  ? - - H:此帮助
  -v:显示版本并退出
  -V:显示版本和配置选项,然后退出
  -t:测试配置和退出
  Q:抑制配置测试在非错误信息
  -s信号:将信号发送到主进程:停止,退出,重新打开,重装
  -p前缀:设置前缀路径(默认:/application/nginx-1.6.3/)
  -c文件名:设置配置文件(默认:CONF / nginx.conf)
  -g指令:设置全局指令进行配置文件
  优化一:优化配置文件
  1、规范优化Nginx配置文件
  # cp nginx.conf{,ori.1}
  # vim nginx.conf
  # cat nginx.conf
  worker_processes1;
  events {
  worker_connections1024;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  sendfile      on;
  keepalive_timeout65;
  #nginx vhosts config
  include extra/www.conf;
  include extra/bbs.conf;
  include extra/blog.conf;
  }
  2、创建extra目录
  # mkdir extra
  a、
  # sed -n '10,17p' nginx.conf.base-name
  server {
  listen       80;
  server_namewww.etiantian.org;
  location / {
  root   html/www;
  indexindex.html index.htm;
  }
  }
  # sed -n '10,17p' nginx.conf.base-name >extra/www.conf
  # cat extra/www.conf
  b、
  # sed -n '18,25p' nginx.conf.base-name
  # sed -n '18,25p' nginx.conf.base-name >extra/bbs.con
  # cat extra/bbs.conf
  c、
  # sed -n '26,33p' nginx.conf.base-name
  # sed -n '26,33p' nginx.conf.base-name >extra/blog.con
  # cat extra/blog.conf
  3、检查语法
  # ../sbin/nginx -t
  4、重启

  # ../sbin/nginx -s>  5、查看本地/etc/hosts里面是否有域名解析
  # cat /etc/hosts
  如果没有的话,加上域名解析
  # vim /etc/hosts
  10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org
  6、检查
  # curl www.etiantian.org
  www.etiantian.org
  # curl bbs.etiantian.org
  bbs.etiantian.org
  # curl blog.etiantian.org
  blog.etiantian.org
  优化二:别名设置
  1、修改配置
  # vi extra/www.conf
  server_namewww.etiantian.org;                #修改前
  server_namewww.etiantian.org etiantian.org;    #修改后
  2、增加域名解析
  # vim /etc/hosts
  10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org                  #修改前
  10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org   #修改后
  3、访问检查
  # curl etiantian.org
  www.etiantian.org
  4、总结:利用别名可以少一次请求,不用跳转
  # curl -I 51cto.com
  HTTP/1.1 301 Moved Permanently
  # curl -I etiantian.org
  HTTP/1.1 200 OK
  # curl -I baidu.com
  HTTP/1.1 200 OK
  优化三:配置Nginx status
  1、
  cat >>/application/nginx/conf/extra/status.conf../logs/error.log

  # ../sbin/nginx -s>  # cat ../logs/error.log
  2016/05/12 19:08:06 12914#0: signal process started
  访问日志
  # sed -n '21,23p' nginx.conf.default
  #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  #                  '$status $body_bytes_sent "$http_referer" '
  #                  '"$http_user_agent" "$http_x_forwarded_for"';
  把#号去掉放进配置文件里
  # cat nginx.conf
  worker_processes1;
  error_log logs/error.log error;
  events {
  worker_connections1024;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  sendfile      on;
  keepalive_timeout65;
  log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  #nginx vhosts config
  include extra/www.conf;
  include extra/bbs.conf;
  include extra/blog.conf;
  include extra/status.conf;
  }
  优化五:访问日志
  最好基于虚拟主机
  # vim extra/www.conf
  # cat extra/www.conf
  server {
  listen       80;
  server_namewww.etiantian.org etiantian.org;
  location / {
  root   html/www;
  indexindex.html index.htm;
  }
  access_log logs/access_www.log main;#新增行
  }
  查看
  # tail -f ../logs/access_www.log
  10.0.0.1 - - "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36" "-"
  10.0.0.1 - - "GET /favicon.ico HTTP/1.1" 404 570 "http://10.0.0.7/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36" "-"

页: [1]
查看完整版本: Nginx安装与优化