前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >程序设计理念-CentOs7实践Nginx-带来安装服务的通用法则

程序设计理念-CentOs7实践Nginx-带来安装服务的通用法则

作者头像
needrunning
发布2020-07-21 23:11:37
5060
发布2020-07-21 23:11:37
举报
文章被收录于专栏:图南科技图南科技

本文记录 Linux CentOS7 环境安装 Nginx 的基本步骤,最后输出 Linux 上安装服务的通用法则。读完本文你将收获

  • 故障排查万能公式
  • 安装服务的通用法则

在新机器上安装 Nginx 服务,与安装 PHP 服务类似,有两种方式

  • 一种是源码编译 官方Nginx[1] 下载对应的版本,解压安装
  • 一种是包安装

官方资料查看这里Nginx 官方资料[2]

Nginx安装

无论采用哪种方式,都需要在官网确定将要安装的服务版本,确定软件源。

编译安装参数参考

代码语言:javascript
复制
--prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/usr/local/nginx/tmp/client_body --http-proxy-temp-path=/usr/local/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi --http-scgi-temp-path=/usr/local/nginx/tmp/scgi --pid-path=/usr/local/nginx/nginx.pid --lock-path=/usr/local/nginx/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module

编译过程中,难免缺少必要的组件,缺什么补什么即可。

服务安装后的路径都在配置文件中设置

  • 配置文件/etc 下
  • 运行文件 /usr/local/nginx
  • 日志文件 /var/log/nginx

将一个服务的不同部分分散到不同的位置

这一点是 Linux 上安装服务与 Windows 上安装服务最大的思维不同。

Nginx 中的常用命令

检查配置文件

代码语言:javascript
复制
 ./nginx  -t;
 nginx: the configuration file /etc/nginx/conf/nginx.conf syntax is ok
 nginx: configuration file /etc/nginx/conf/nginx.conf test is successful

检查版本

代码语言:javascript
复制
 ./nginx  -v;
 nginx version: nginx/1.15.2

检查模块

代码语言:javascript
复制
 ./nginx  -V;
nginx version: nginx/1.15.2

重启服务 ./nginx -s reload

常规配置

Nginx 下 PHP 配置

代码语言:javascript
复制
server {
    listen  80;    
    server_name xx.cn;    
    index index.php index.html index.htm;    
    root  /data/deploy/xx/backend/web;
    
    location ~* /\. {    
         deny all;    
    }
    
    location  / {    
        try_files $uri /index.php?$args;    
    }    
    location ~ .*\.(php|php5)?$    
    {         
    fastcgi_pass    127.0.0.1:9000;         
    fastcgi_index   index.php;         
    fastcgi_split_path_info ^(.+\.php)(/.+)$;         
    fastcgi_param  SCRIPT_FILENAME    
$document_root$fastcgi_script_name;    
    include         fastcgi_params;    
    }    
    client_max_body_size 512m;
}
  • try_files
代码语言:javascript
复制
 location / {       
      # Redirect everything that isn't a real file to index.php            
      try_files $uri $uri/ /index.php$is_args$args;        
 }

参考 nginx try_files[3]

故障排查万能公式

查看 Nginx 运行状态,以及故障,从以下 4 个方面检查

  • user
  • error_log
  • pid
  • access_log

运行 Nginx 的用户,及权限 查看错误日志输出 pid 是否生成 查看是否有访问日志

对于应用服务的排查,以上 4 个方面是一个万能公式。

技术背景 504

LNMP 环境下,反向代理服务器 Nginx 错误日志大量报错,显示 504

nginx504.png

upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time,

or established connection failed because connected host has failed to respond) while reading response header from upstream

理论回看

Nginx 504 Gateway Time-out 的含义是所请求的网关没有请求到。

简单来说就是没有请求到可以执行的 PHP-CGI

提交动态请求的时候,Nginx 会直接把 请求转交给 php-fpm。

而 php-fpm 再分配 php-cgi 进程来处理相关的请求。

之后再依次返回,最后由 Nginx 把结果反馈给客户端浏览器。

原因列举如下

  • 1 作为服务调用方,服务提供方接口异常,造成访问超时

Nginx 交由 PHP 处理的任务长时间没有返回,Nginx 直接返回 504。

而这种超时,在 PHP 调用层面的的代码逻辑里很难捕获到,并且习惯上也不捕获,交由框架层面的异常捕获器。

  • 2 作为服务提供者,处理严重耗时

自身服务访问数据库超时不返回或者 PHP 层面业务处理严重耗时。

程序设计不合理,造成长时间延迟超时。

fastcgi进程

问题追踪

如果 PHP 服务出现短时间大量 504 错误,会把整个 fastcgi 通道拥塞堵死。

最后就是 PHP 服务挂了。

服务相互影响

如果多个服务以虚拟主机的形式在同一台服务器上,这多个服务都会受到影响。

最直观的前台体验就是访问速度慢,或者直接打不开。

定时任务,跑批,数据库批量数据更新相关业务容易出这种类型的 Nginx 错误。

网络上通过配置解决 504 的方式,正常情况下不建议使用。

优先考虑程序设计和实现方面的不足,参照我之前的一篇博文 PHP性能优化之连接超时如何解

分析总结

web 应用领域,不管是哪种语言,部署应用程序绕不开 Nginx 服务,本文通过 Nginx 服务的安装,

总结出在 Linux 上安装服务的通用法则

  • 1 确定服务器环境
  • 2 确定待安装服务的安装方式和安装版本
  • 3 确定服务运行账户和相应的配置
  • 4 确定服务正常启动运行
  • 5 确定是否自启动

以上同样适用于 Mysql Redis PHP 等服务的安装。

图南日晟-互联网技术服务

参考资料

[1] Nginx: http://nginx.org/

[2] Nginx 官方资料: http://nginx.org/en/linux_packages.html#RHEL-CentOS

[3] Nginx try_files: http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 图南科技 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Nginx 中的常用命令
  • 常规配置
    • Nginx 下 PHP 配置
    • 故障排查万能公式
      • 技术背景 504
        • 理论回看
          • 问题追踪
          • 分析总结
            • 参考资料
            相关产品与服务
            云数据库 Redis
            腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档