Nginx on centos7

来自linux中国网wiki
跳到导航 跳到搜索

base

例如我的安装目录是/usr/local/nginx 
修改配置后重新加载生效 
/usr/local/nginx/sbin/nginx -s reload 
重新打开日志文件 
/usr/local/nginx/sbin/nginx -s reopen 
测试nginx配置文件是否正确 
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf 
启动nginx 
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 
快速停止nginx 
/usr/local/nginx/sbin/nginx -s stop 
完整有序的停止nginx 
/usr/local/nginx/sbin/nginx -s quit 
注:stop和quit的区别在于 
quit是一个优雅的关闭方式,Nginx在退出前完成已经接受的连接请求 
Stop 是快速关闭,不管有没有正在处理的请求。

其他的停止nginx 方式: 
ps -ef | grep nginx

kill -QUIT 主进程号 :从容停止Nginx 
kill -TERM 主进程号 :快速停止Nginx 
pkill -9 nginx :强制停止Nginx

平滑重启nginx: 
kill -HUP 主进程号

为了方便,现将nginx服务添加至systemctl 
1.修改nginx配置文件,开启pid 
pid /var/run/nginx.pid; 
2.通过上述方式关闭nginx服务 
3.配置服务,在/usr/lib/systemd/system/nginx.service文件,vim编辑如下

vi /usr/lib/systemd/system/nginx.service
[Unit] 
Description=nginx - high performance web server 
After=network.target remote-fs.target nss-lookup.target

[Service] 
Type=forking 
PIDFile=/var/run/nginx.pid 
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf 
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 
ExecReload=/usr/local/nginx/sbin/nginx -s reload 
ExecStop=/usr/local/nginx/sbin/nginx -s stop 
ExecQuit=/usr/local/nginx/sbin/nginx -s quit 
PrivateTmp=true

[Install] 
WantedBy=multi-user.target


[Unit]部分主要是对这个服务的说明,内容包括Description和After,Description用于描述服务,After用于描述服务类别

[Service]部分是服务的关键,是服务的一些具体运行参数的设置,这里Type=forking是后台运行的形式,PIDFile为存放PID的文件路径,ExecStart为服务的具体运行命令,ExecReload为重启命令,ExecStop为停止命令,PrivateTmp=True表示给服务分配独立的临时空间,注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错!

[Install]部分是服务安装的相关设置,可设置为多用户的

服务脚本按照上面编写完成后,以754的权限保存在/usr/lib/systemd/system目录下 
然后执行(修改或新增文件需要执行以下语句才能生效) 
systemctl daemon-reload 
这时就可以利用systemctl进行配置了 
systemctl start/stop/reload/quit nginx.service 
若发生报错,可通过systemctl status nginx.service查看失败原因 
systemctl enable nginx.service 开机自启动

upstream

   upstream read.zhaitua.com {
        server  172.18.140.166:1080 weight=5;
#               server  172.18.140.165:1080 weight=1;
        keepalive       32;
    }   

troubleshooting

原因: 502 GAT away Php-cgi进程挂掉或者是没有cgi进程

504 timeout Nginx请求不到php-cgi进程,超时

解决方法: 思路:

502错误和php-fpm.conf配置文件有关系,网上很多教程都说了,一般就设置以下几个选项

nginx常见问题处理


Nginx 502 Bad Gateway 错误的原因及解决方法

nginx故障及处理


Nginx常见故障 400 502 504

PHP与NGINX 499、502问题处理


Nginx写IO占用高故障处理

complate

./configure --prefix=/data/apps/nginx --user=www --group=www  --error-log-path=/data/logs/nginx/error.log   --http-log-path=/data/logs/nginx/access.log  --pid-path=/data/apps/nginx/nginx\
   .pid --lock-path=/data/apps/nginx/nginx.lock      --with-http_stub_status_module  --with-pcre=/root/rpmbuild/SOURCES/pcre-8.21 --with-zlib=/root/rpmbuild/SOURCES/zlib-1.2.3 --with-openss\
   l=/root/rpmbuild/SOURCES/openssl-1.0.1p  --without-poll_module    --with-http_ssl_module  --with-http_gzip_static_module  --without-poll_module  --without-http_ssi_module --without-http_\
   userid_module --without-http_geo_module --without-http_memcached_module --without-http_map_module --with-http_realip_module   --without-mail_pop3_module  --without-select_module  --witho\
   ut-mail_imap_module --without-mail_smtp_module  --without-http_scgi_module  --with-cc-opt='-O3' --with-ipv6;

Building nginx from Sources

admin-guide

Nginx优化

yum install



RHEL/CentOS

Install the prerequisites:

    sudo yum install yum-utils

To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:

    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true

    [nginx-mainline]
    name=nginx mainline repo
    baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true

By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:

    sudo yum-config-manager --enable nginx-mainline

To install nginx, run the following command:

    sudo yum install nginx

#issue

GPG key retrieval failed: [Errno 14] curl#60 - “Peer‘s Certificate issuer is not recognized.“
 wget https://nginx.org/keys/nginx_signing.key --no-check-certificate
 rpm --import nginx_signing.key
  yum install nginx 


see also

https://nginx.org/en/linux_packages.html

Nginx 反向代理与负载均衡详解


CentOS 7.0 yum安装配置LAMP服务器(Apache+PHP+MariaDB)