nginx 基础知识
安装
http://nginx.org/en/download.html
安装依赖
$ yum -y install pcre pcre-devel
$ yum -y install openssl openssl-devel
$ yum -y install zlib zlib-devel
编译安装
$ ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_ssl_module
$ make && make install
查看nginx的参数
$ nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-pata=/usr/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=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_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-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
参数的详细说明:
http://nginx.org/en/docs/configure.html
从 --sbin-path=/usr/sbin/nginx
得知nginx的启动位置
启动
$ sudo /usr/sbin/nginx
# 检测80端口
$ sudo netstat -tunpl | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4563/nginx: master
tcp6 0 0 :::80 :::* LISTEN 4563/nginx: master
浏览器访问:http://localhost
看到的文件在 --prefix=/usr/share/nginx
参数设置的路径下的html目录:
$ ls /usr/share/nginx/html
404.html 50x.html index.html nginx-logo.png poweredby.png
在该目录下,新增一个test.html测试
nginx指令
查看nginx指令使用帮助
$ sudo /usr/sbin/nginx -h
nginx version: nginx/1.12.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/share/nginx/)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
-
nginx –s stop 停止nginx的服务
-
nginx –s reload 不停止nginx的服务,重新加载配置文件。
-
nginx –t 检测配置文件是否有错误。
-
nginx -V查看Nginx编译时的参数?
用信号量控制nginx
不常用,一般用nginx自带的命令即可
https://www.nginx.com/resources/wiki/start/topics/tutorials/commandline/
(1)比如立即关闭进程:
kill –INT nginx的主进程号
查看某个任务的进程号,可以用ps aux|grep nginx
(2)优雅的关闭进程(即等请求结束后,再关闭)
kill –QUIT nginx的主进程号
(3)重读配置文件
kill –HUP nginx的主进程号与nginx –s reload一样
把配置文件中的新更新的东西加载到正在运行的nginx的进程中,接着对用户提供服务,但是nginx的进程并没有关闭。即重读配置文件。
可以通过修改配置文件进行测试
(4)重读日志文件
Kill -USR1 cat /xxx/path/log/nginx.pid
重新读日志文件,日志文件改名备份后,使用,否则仍然写入原来的日志文件。
(5)可以使用nginx.pid文件代替nginx的主进程号
查看nginx.pid文件的位置,打开配置文件进行查看:
即:kill –信号控制 cat /xxx/path/log/nginx.pid
优雅的关闭:等待请求完成后再关闭
用信号量关闭进程
$ sudo kill -INT 4563
用信号管理比较麻烦,因为要事先知道进程号
也能通过nginx.pid文件查看进程号,该文件在配置文件中指定
$ cat /run/nginx.pid
4911
$ ps aux | grep nginx
root 4911 0.0 0.1 120744 2248 ? Ss 09:50 0:00 nginx: master process /usr/sbin/nginx
nginx 4912 0.0 0.1 121128 3104 ? S 09:50 0:00 nginx: worker process
nginx 4913 0.0 0.1 121128 3104 ? S 09:50 0:00 nginx: worker process
直接读取文件
$ kill -USR1 `cat /run/nginx.pid`
- 上一篇: 大型网站优化思路
- 下一篇: mysql 开启查询缓存优化查询速度
