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

[经验分享] nginx初级安装配置

[复制链接]

尚未签到

发表于 2018-11-15 08:12:07 | 显示全部楼层 |阅读模式
  实验环境:系统 CENTOS5.5,用户 root
  目录以yum默认安装为参考
  一、安装
  1、安装epel源(安装时注意版本选择,此处为5.X 32位版)
  rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel//5/i386/epel-release-5-4.noarch.rpm
  安装nginx以及php模块
  yum install nginx php-fpm php-cgi
  2、如果以前用的是apache,记得要停掉
  /etc/init.d/httpd stop
  chkconfig httpd --level 2345 off
  3、启动nginx
  chkconfig httpd --level 2345 on
  /etc/init.d/nginx start
  二、常用操作
  1、启动
  /usr/sbin/nginx -c /etc/nginx/nginx.conf
  不使用-c指定配置文件的话,默认加载安装目录下conf/nginx.conf
  2、停止
  平衡停止
  kill -QUIT
  或
  kill -QUIT `cat /var/run/nginx.pid`
  3、重启

  /usr/sbin/nginx -s>  或
  kill -HUP `cat /var/run/nginx.pid`
  4、配置文件检查
  /usr/sbin/nginx -t -c /etc/nginx/nginx.conf
  5、查看版本
  简单显示版本号
  /usr/sbin/nginx -v
  详细显示版本及config相关信息
  /usr/sbin/nginx -V
  三、基本配置
  1、nginx.conf基础配置
#工作用户及用户组(根据机器环境修改配置)  
user nginx nginx;
  
#工作进程数(一般设为CPU总核数或其两倍)
  
worker_processes 8;
  
#错误日志路径及记录级别(debug,info,notice,warn,error,crit)
  
error_log /var/log/nginx/error.log warn;
  
#pid保存路径
  
pid /var/run/nginx.pid;
  
#文件描述符数
  
worker_rlimit_nofile 51200;
  
events
  
{
  #使用的网络I/O模型,linux推荐epoll模型,freebsd推荐kqueue模型
  use epoll;
  #允许的连接数,可以的话尽量设大一些
  worker_connections 51200;
  
}
  
http
  
{
  include /etc/niginx/mime.types;
  defaut_type application/octet-stream;
  #默认字符集,如不确定网站字符集,则不要设置,通过html的meta标签指定。
  charset utf-8;
  #禁止错误页面里显示nginx的版本号
  server_tokens off;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  #客户端上传文件大小限制
  client_max_body_size 8m;
  sendfile on;
  tcp_nopush on;
  #客户端连接超时,服务器将关闭连接。
  keepalive_timeout 60;
  tcp_nodelay on;
  #开启gzip压缩
  gzip on;
  #小于设置大小的文件不压缩
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.1;
  #压缩等级
  gzip_comp_level 2;
  #压缩文件的类型
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  #指定是否传递错误信息到客户端,或者允许nginx使用error_page处理错误信息。
  fastcgi_intercept_errors off;
  server
  {
  #详见 站点配置
  }
  
}
  2、去除fastcgi中nginx版本号相关设置
  编辑/usr/local/nginx/conf/fastcgi.conf和/usr/local/nginx/conf/fastcgi_params
  将fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;改成fastcgi_param SERVER_SOFTWARE nginx;
  3、防止被绑定域名
  server {
  listen 80 default;
  rewrite ^.* http://www.test.com permanent; #此处域名指向自己主站,以引导网友访问正确网站。
  }
  4、禁止某目录下的某类文件访问
  location ~* ^/()/.*\.()$
  {
  deny all;
  }
  例如:
  location ~* ^/(images|files)/.*\.(php|php3|php4|php5|cgi)$
  {
  deny all;
  }
  5、防止php_info BUG
  编辑/usr/local/nginx/conf/fastcgi_params,首行添加如下内容
  if ($request_filename ~* (.*)\.php)
  {
  set $php_url $1;
  }
  if (!-e $php_url.php)
  {
  return 403;
  }
  6、防旁注
  各个虚拟主机目录设为不易猜测的名字。
  在php.ini中修改open_basedir值,设置为上传文件的临时目录和各虚拟主机存放目录(以冒号分隔)。
  chmod 755 -R
  chmod 711
  7、防盗链
  以防图片盗链为例,只要请求来源非www.test.com则一概返回403错误.
  location ~* .(gif|jpg|png)$ {
  valid_referers none blocked www.test.com;
  if ($invalid_referer) {
  return 403;
  }
  }
  四、站点配置
  每组server{}是一个虚拟主机。
  1、基于IP的虚拟主机
server  
{
  #监听IP及端口
  listen 192.168.1.2:80;
  #主机名
  server_name 192.168.1.2;
  #日志文件存放路径
  access_log logs/host.access.log main;
  location /
  {
  #默认首页文件,从左至右匹配
  index index.html index.htm;
  #网站目录
  root /opt/web/server1;
  }
  
}
  2、基于域名的虚拟主机
server  
{
  #监听端口
  listen 80;
  #主机名,表示处理所有test1.test.com、test2.test.com以及*.test.cn域名的访问。
  server_name test1.test.com test2.test.com *.test.cn;
  #日志文件存放路径
  access_log logs/host.access.log main;
  location /
  {
  index index.html index.htm;
  root /opt/web/server1;
  }
  
}
  五、日志处理
  1、log_format
  基本格式:log_format  
  示例:log_format gzip '$remote_addr - $remote_user [$time_local] '
  '"$request" $status $bytes_sent '
  '"$http_referer" "$http_user_agent" "$gzip_ratio"';
  2、access_log
  基本格式: access_log  [日志格式名(通过log_format定义)] [buffer=SIZE];
  示例:access_log /var/log/web/test.log gzip buffer=32k;
  3、日志文件切割
  ·创建脚本,输入以下内容,保存到恰当的位置,例如/opt/cut_log.sh
  #! /bin/bash
  #功能说明:自动分割压缩日志文件,保存最近15天日志
  EXPIRES_DAY=15
  BAK_NAME=$(date -d "yesterday" +"%Y%m%d")
  LOG_PARENT_PATH="/var/log/nginx/"
  LOG_PATH="access/"
  mkdir -p ${LOG_PARENT_PATH}${LOG_PATH}
  mv ${LOG_PARENT_PATH}access.log ${LOG_PARENT_PATH}${LOG_PATH}${BAK_NAME}.log
  kill -USR1 `cat /var/run/nginx.pid`
  cd ${LOG_PARENT_PATH}${LOG_PATH} && tar -czf ${BAK_NAME}.tgz ${BAK_NAME}.log
  rm -f ${LOG_PARENT_PATH}${LOG_PATH}${BAK_NAME}.log
  find ${LOG_PARENT_PATH}${LOG_PATH} -name '*.tgz' -ctime ${EXPIRES_DAY} -exec rm {} \;
  ·添加到计划任务,每天0时0分自动执行
  crontab -u root -e
  0 0 * * * /opt/cut_log.sh
  六、rewrite处理
  1、 if
  基本格式:if (){...}
  不能嵌套,不能多条件判断
  支持的判断条件:
  ~ 区分大小写
  ~* 不区分大小写
  -f 判断文件是否存在
  -d 判断目录是否存在
  -e 判断文件或目录是否存在
  -x 判断文件是否可执行
  判断符前加!表示不匹配,如!-f 表示匹配文件不存在
  和括号中正则匹配的内容,之后可以用变量$1至$9调用,比如:
  if ($request_uri ~* "/test/(\d+)\.html")
  {
  set $numb $1;
  rewrite ^(.*)$ /msie/$1/$numb.html break;
  }
  注:上例中有两个匹配项,set $numb $1;中的$1匹配的是"/test/(\d+)\.html"中的(\d+)。
  rewrite ^(.*)$ /msie/$1/$numb.html break;中的$1匹配的是(.*)。
  2、return
  基本格式:return
  返回状态码,包括204、400、402~406、408、410、411、413、416和500~504。
  3、rewrite
  基本格式:rewrite   
  重定向符合标准的链接至修改后的链接。
  重写表达式只对相对路径有效。例如:
  #访问URL: http://www.test.com/main/index.htm
  if ($host ~* www\.(.*))
  {
  set $host_without_www $1;
  rewrite ^(.*)$ http://$host_without_www$1 permanent; #此处$1的内容为"/main/index.htm"
  }
  标志定义:
  last:完成rewrite,使用alias指令时必须用此标记。
  break:此规则匹配完成后,终止匹配。使用proxy_pass指令时使用此标记。
  redirect:302重定向。
  permanent:301重定向。
  last在本条rewrite规则执行完后,会对所在的server{}重新发请求,而break则在匹配完后终止匹配。
  因此,一般在location /{}或直接在server{}中的rewrite规则里使用last,在非根location中使用break。
  如URI中有参数(例:http://www.test.com/index.htm?id=10),默认情况参数会自动附到替换串上,如果不需要附带参数,
  则在替换串末尾加上“?”。
  4、set
  基本格式:set  
  用于定义变量或变量赋值。例如:
  set $name 'lykyl';
  附: nginx支持的信号
  TERM INT 快速关闭
  QUIT 从容关闭
  HUP 平滑重启
  USR1 重新打开或建立日志文件
  USR2 平滑升级可执行程序
  WINCH 从容关闭工作进程
  附:nginx日志变量
  $body_bytes_sent the number of bytes sent to a client not counting the response header; this variable is
  compatible with the “%B” parameter of the mod_log_config Apache module
  $bytes_sent the number of bytes sent to a client
  $connection connection serial number
  $connection_requests the current number of requests made through a connection
  $msec time in seconds with a milliseconds resolution at the time of log write
  $pipe “p” if request was pipelined, “.” otherwise
  $request_length request length (including request line, header, and request body)
  $request_time request processing time in seconds with a milliseconds resolution; time elapsed between the
  first bytes were read from the client and the log write after the last bytes were sent to the client
  $status response status
  $time_iso8601 local time in the ISO 8601 standard format
  $time_local local time in the Common Log Format
  附:nginx rewrite全局变量
  $arg_PARAMETER 包含GET请求中,如果有变量PARAMETER时的值。
  $args 请求行中(GET请求)的参数。
  $binary_remote_addr #二进制的客户地址。
  $body_bytes_sent #响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。
  $content_length #请求头中的Content-length字段。
  $content_type #请求头中的Content-Type字段。
  $cookie_COOKIE #cookie COOKIE变量的值
  $document_root #当前请求在root指令中指定的值。
  $document_uri #与$uri相同。
  $host #请求主机头字段,否则为服务器名称。
  $hostname #Set to the machine’s hostname as returned by gethostname
  $http_HEADER
  $is_args #如果有$args参数,这个变量等于”?”,否则等于”",空值。
  $http_user_agent #客户端agent信息
  $http_cookie #客户端cookie信息
  $limit_rate #这个变量可以限制连接速率。
  $query_string #与$args相同。
  $request_body_file #客户端请求主体信息的临时文件名。
  $request_method #客户端请求的动作,通常为GET或POST。
  $remote_addr #客户端的IP地址。
  $remote_port #客户端的端口。
  $remote_user #已经经过Auth Basic Module验证的用户名。
  $request_completion #如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。
  $request_method #GET或POST
  $request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。
  $request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。
  $scheme #HTTP方法(如http,https)。
  $server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
  $server_addr #服务器地址,在完成一次系统调用后可以确定这个值。
  $server_name #服务器名称。
  $server_port #请求到达服务器的端口号。
  $uri #不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。该值有可能和$request_uri 不一致。
  $request_uri是浏览器发过来的值。该值是rewrite后的值。例如做了internal redirects后。
  (lykyl原创,转载请注明出处)



运维网声明 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-635181-1-1.html 上篇帖子: Nginx配置错误页面 下篇帖子: Nginx、Haproxy、LVS负载均衡从原理到部署(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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