Nginx安装
Nginx版本描述:
Mainline version 开发版
Stable version 稳定版
Legacy version 历史版本
基础环境准备:
确认系统网络
[root@Nginx ~]# ping baidu.com
关闭firewalld
[root@Nginx ~]# systemctl stop firewalld
[root@Nginx ~]# systemctl disable firewalld
临时关闭selinux
[root@Nginx ~]# setenforce 0
初始化基本目录
[root@Nginx ~]# mkdir /soft/{code,logs,package,backup} -p
基本安装包
[root@Nginx ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
配置Nginx官⽅方Yum源
官方文档安装步骤: http://nginx.org/en/linux_packages.html
[root@Nginx ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
安装Nginx
[root@Nginx ~]# yum install nginx -y
查看Nginx当前版本
[root@Nginx ~]# nginx -v
nginx version: nginx/1.12.2
Nginx安装目录
[root@Nginx ~]# rpm -ql nginx
如下对 Nginx 安装目录做详细概述
配置⽂文件Nginx主配置⽂文件
/etc/nginx
/etc/nginx/nginx.conf
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
配置文件Cgi、Fastcgi、Uwcgi配置文件
/etc/nginx/fastcgi_params
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
配置文件Nginx编码转换映射文件
/etc/nginx/win-utf
/etc/nginx/koi-utf
/etc/nginx/koi-win
配置文件http协议的Content-Type与扩展名
/etc/nginx/mime.types
配置文件配置系统守护进程管理器
/usr/lib/systemd/system/nginx.service
配置文件Nginx日志轮询,日志切割
/etc/logrotate.d/nginx
命令Nginx终端管理命令
/usr/sbin/nginx
/usr/sbin/nginx-debug
目录Nginx模块目录
/etc/nginx/modules
/usr/lib64/nginx
/usr/lib64/nginx/modules
目录Nginx默认站点目录
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
目录Nginx的帮助手册
/usr/share/doc/nginx-1.12.2
/usr/share/man/man8/nginx.8.gz
目录Nginx的缓存目录
/var/cache/nginx
目录Nginx的日志目录
/var/log/nginx
Nginx编译参数
查看Nginx编译参数
[root@Nginx ~]# nginx -V
下表展示了Nginx编译参数选项以及作用
Nginx常用模块
Nginx模块分为 Nginx官方模块以及Nginx第三方模块
Nginx内置变量
http核心模块的内置变量
http请求变量
Nginx内置变量
自定义变量量
$uri: 当前请求的uri,不带参数
$request_uri: 请求的uri,带完整参数
$host: http请求报文中host⾸部,如果没有则以处理此请求的虚拟主机的主机名代替
$hostname: nginx服务运行在主机的主机名
$remote_addr: 客户端IP
$remote_port: 客户端口
$remote_user: 使用户认证时客户端用户输入的用户名
$request_filename: 用户请求中的URI经过本地root或alias转换后映射的本地文件路径
$request_method: 请求方法, GET POST PUT
$server_addr: 服务器地址
$server_name: 服务器名称
$server_port: 服务器端口
$server_protocol: 服务器向客户端发送响应时的协议, 如http/1.1 http/1.0
$scheme:在请求中使⽤用scheme, 如http://xxx.com中的http
$http_HEADER: 匹配请求报文中指定的HEADER
$http_host: 匹配请求报文中的host⾸首部
$document_root: 当前请求映射到的root配置
Nginx编译安装
openresty编译安装
1.基础环境准备
mkdir /soft/package/src -p
mkdir /soft/package/src/ngx_3rd -p
useradd -s /sbin/nologin -M www
yum -y install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel kernel keyutils patch perl zlib tcl readline-devel glic libxslt-devel gd-devel GeoIP-devel libevent libevent-devel
2.安装依赖插件
cd /soft/package/src
openresty安装包
wget https://openresty.org/download/openresty-1.11.2.2.tar.gz
tar -xf openresty-1.11.2.2.tar.gz
libdrizzle模块
wget http://openresty.org/download/drizzle7-2011.07.21.tar.gz
tar xzvf drizzle7-2011.07.21.tar.gz
cd drizzle7-2011.07.21/
./configure --without-server
make libdrizzle-1.0
make install-libdrizzle-1.0
openssl依赖包安装
wget https://www.openssl.org/source/openssl-1.0.2d.tar.gz
tar -xf openssl-1.0.2d.tar.gz
pcre依赖
wget -O pcre-8.37.tar.gz \
http://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz/download
tar -xf pcre-8.37.tar.gz
wget ftp://91.193.69.2/distributive/FreeBSD/ports/local-distfiles/osa/nginx-accessk
ey-2.0.3.tar.gz
tar xf nginx-accesskey-2.0.3.tar.gz
mv nginx-accesskey-2.0.3 ngx_3rd/nginx-accesskey-master
cd /soft/package/src/ngx_3rd
wget -O ngx_http_consistent_hash.zip https://codeload.github.com/replay/ngx_http_consistent_hash/zip/master
unzip ngx_http_consistent_hash.zip
wget -O nginx-static-etags.zip https://codeload.github.com/mikewest/nginx-static-etags/zip/master
unzip nginx-static-etags.zi wget -O nginx-http-footer-filter.zip https://codeload.github.com/alibaba/nginx-http-footer-filter/zip/master
unzip nginx-http-footer-filter.zip
wget -O form-input-nginx-module.zip https://codeload.github.com/calio/form-input-nginx-module/zip/master
unzip form-input-nginx-module.zip
wget -O ngx_http_accounting_module.zip https://codeload.github.com/Lax/ngx_http_accounting_module/zip/master
unzip ngx_http_accounting_module.zip
wget -O ngx_log_if.zip https://codeload.github.com/cfsego/ngx_log_if/zip/master
unzip ngx_log_if.zip
wget -O nginx-limit-upstream.zip https://codeload.github.com/cfsego/nginx-limit-upstream/zip/master
unzip nginx-limit-upstream.zip
wget -O limit_upload_rate.zip https://codeload.github.com/cfsego/limit_upload_rate/zip/master
unzip limit_upload_rate.zip
wget -O ngx_devel_kit.zip https://codeload.github.com/simpl/ngx_devel_kit/zip/master
unzip ngx_devel_kit.zip
wget -O nginx-module-vts.zip https://codeload.github.com/vozlt/nginx-module-vts/zip/master
unzip nginx-module-vts.zip
wget -O nginx_tcp_proxy_module.zip https://codeload.github.com/yaoweibin/nginx_tcp_proxy_module/zip/master
unzip nginx_tcp_proxy_module.zip
wget -O nginx-module-sts.zip https://codeload.github.com/vozlt/nginx-module-sts/zip/master
unzip nginx-module-sts.zip
wget -O nginx-http-concat.zip https://codeload.github.com/alibaba/nginx-http-concat/zip/master
unzip nginx-http-concat.zip
wget -O nginx-url.zip https://codeload.github.com/vozlt/nginx-module-url/zip/master
unzip nginx-url.zip
wget -O nginx-access-plus.zip https://codeload.github.com/nginx-clojure/nginx-access-plus/zip/master
unzip nginx-access-plus.zip
3.编译openresty
cd /soft/package/src/ngx_openresty-1.11.2.2
./configure \
--user=www \
--group=www \
--prefix=/soft/openresty-1.11 \
--pid-path=/soft/openresty-1.11/nginx/pid \
--error-log-path=/soft/log/nginx/error.log \
--http-log-path=/soft/log/nginx/access.log \
--http-proxy-temp-path=/soft/openresty-1.11/nginx/proxy_temp \
--http-fastcgi-temp-path=/soft/openresty-1.11/nginx/fastcgi_temp \
--http-client-body-temp-path=/soft/openresty-1.11/nginx/client_body_temp \
--with-openssl=/soft/package/src/openssl-1.0.2d \
--with-pcre=/soft/package/src/pcre-8.37 \
--with-libdrizzle=/usr/local \
--with-threads \
--with-file-aio \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_ssl_module \
--with-http_xslt_module \
--with-http_iconv_module \
--with-http_geoip_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_drizzle_module \
--with-http_addition_module \
--with-http_gzip_static_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_image_filter_module \
--without-http_uwsgi_module \
--without-http_scgi_module \
--with-mail \
--with-mail_ssl_module \
--add-module=/soft/package/src/ngx_3rd/nginx-accesskey-master/ \
--add-module=/soft/package/src/ngx_3rd/ngx_http_consistent_hash-master/ \
--add-module=/soft/package/src/ngx_3rd/nginx-static-etags-master/ \
--add-module=/soft/package/src/ngx_3rd/nginx-http-footer-filter-master/ \
--add-module=/soft/package/src/ngx_3rd/ngx_http_accounting_module-master/ \
--add-module=/soft/package/src/ngx_3rd/ngx_log_if-master/ \
--add-module=/soft/package/src/ngx_3rd/nginx-http-concat-master/ \
--add-module=/soft/package/src/ngx_3rd/nginx-module-vts-master/ \
--add-module=/soft/package/src/ngx_3rd/nginx-module-url-master/ \
--add-module=/soft/package/src/ngx_3rd/nginx-access-plus-master/src/c/
gmake && gmake install && ln -s /soft/openresty-1.11 /soft/openresty