nginx的yum安装
从nginx官网获取源
vim /etc/yum.repose.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
OS改为centos
OSRELEASE改为当前系统版本
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
yum install ngnx
rpm -ql nginx
[root@localhost110 sbin]# rpm -ql nginx
/etc/logrotate.d/nginx 配置文件 nginx日志轮转,用于logrotata服务的日志切割
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/rc.d/init.d/nginx
/etc/rc.d/init.d/nginx-debug
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.12.
/usr/share/doc/nginx-1.12./COPYRIGHT
/usr/share/man/man8/nginx..gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx
目录说明
/etc/logrotate.d/nginx 配置文件 nginx日志轮转,用于logrotata服务的日志切割
目录配置文件
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/nginx.conf
配置文件cgi,fastcgi相关配置
/etc/nginx/fastcgi_params
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
配置文件,编码转换映射转化文件
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/win-utf
配置文件,设置http协议的content-type与扩展名对应文件
/etc/nginx/mime.types
用于配置出系统守护进程管理器管理方式
/etc/rc.d/init.d/nginx
/etc/rc.d/init.d/nginx-debug
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
nginx模块目录
/etc/nginx/modules
/usr/lib64/nginx/modules
nginx服务的启动管理终端命令
/usr/sbin/nginx
/usr/sbin/nginx-debug
手册和帮助文件
/usr/share/doc/nginx-1.12.1
/usr/share/doc/nginx-1.12.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
缓存目录
/var/cache/nginx
日志目录
/var/log/nginx
2.nginx的编译参数
nginx -V
[root@localhost110 sbin]# ./nginx -V
nginx version: nginx/1.12.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/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 --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
基础路径
--prefix=/etc/nginx
--sbin-path=/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
--pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock
执行对应模块时,nginx保留的临时目录
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
用户或组
--user=nginx --group=nginx
--with-compat --with-file-aio --with-threads --with-http_addition_module
--with-http_auth_request_module --with-http_dav_module --with-http_flv_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_mp4_module --with-http_random_index_module
--with-http_realip_module --with-http_secure_link_module
--with-http_slice_module --with-http_ssl_module
--with-http_stub_status_module --with-http_sub_module --with-http_v2_module
--with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module
--with-stream_ssl_module --with-stream_ssl_preread_module
设置额外的参数将被添加到CFLAGS变量
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC'
设置的附加参数,链接到系统库
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
nginx的基础配置语法
user:设置nginx服务的系统使用用户
worker_processes:工作进程数(和cpu核心数保持一致最好)
error_log:错误日志
pid:nginx服务启动时的Pid
events {
worker_connections 每个进程的最大连接数
use io模型
}
curl -v 查看请求
curl -v http://192.168.1.251/1.html
* About to connect() to 192.168.1.251 port (#)
* Trying 192.168.1.251... connected
* Connected to 192.168.1.251 (192.168.1.251) port (#)
> GET /.html HTTP/1.1
> User-Agent: curl/7.19. (x86_64-redhat-linux-gnu) libcurl/7.19. NSS/3.21 Basic ECC zlib/1.2. libidn/1.18 libssh2/1.4.
> Host: 192.168.1.251
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.12.1
< Date: Fri, 06 Oct 2017 14:11:25 GMT
< Content-Type: text/html
< Content-Length: 7
< Last-Modified: Fri, 06 Oct 2017 14:10:08 GMT
< Connection: keep-alive
< ETag: "59d78ec0-7"
< Accept-Ranges: bytes
<
1.html
* Connection #0 to host 192.168.1.251 left intact
* Closing connection #0 curl -v http://192.168.1.251/1.html >/dev/null
* About to connect() to 192.168.1.251 port 80 (#0)
* Trying 192.168.1.251... connected
* Connected to 192.168.1.251 (192.168.1.251) port 80 (#0)
> GET /1.html HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.1.251
> Accept: */*
>
< HTTP/1.1 OK
< Server: nginx/1.12.
< Date: Fri, Oct :: GMT
< Content-Type: text/html
< Content-Length:
< Last-Modified: Fri, Oct :: GMT
< Connection: keep-alive
< ETag: "59d78ec0-7"
< Accept-Ranges: bytes
<
{ [data not shown]
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
--:--:-- --:--:-- --:--:-- * Connection # to host 192.168.1.251 left intact * Closing connection #
页面定制
error_page /.html;
location = /.html{
return "not find!";
}
error_page /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
比如关闭php-fpm后产生502会定位到/50x.html return http状态码 string
return 内容
return 跳转到的链接
location ~ ^/getjson{
default_type application/json;
return '{"status":1,"note":"ok"}';
} location ~* ^/nums/(\d+)([a-z]+)\.html {
set $d $;
set $s $;
return str:$d$s;
}
http://192.168.1.251/nums/2017abcE.html
str:2017abcE
#防止乱码
location /txt {
default_type text/html;
add_header Content-Type 'text/html;charset=utf-8';
return "nginx你好";
}
日志类型
error.log access_log
log_format
Syntax: log_format name [escape=default|json] string .....;
Default: log_format combined "....";
Context:http log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main;
nginx变量
HTTP请求变量
arg_PARAMETER,http_HEADER,sent_http_HEADER
内置变量: nginx内置的
自定义变量:自己定义
模块安装
比如要安装 :ngx_http_realip_module
获取之前的在安装编译参数
nginx -V
--group=nginx --user=nginx --prefix=/usr/local/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.41
重新编译,编译参数后面加上
--with-http_realip_module
进入源码目录
./configure
--group=nginx --user=nginx --prefix=/usr/local/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.41 --with-http_realip_module
make
不make install
make完之后在objs目录下就多了个nginx
替换现有的nginx即可