CentOS 7 安装 nginx 教程
安装 nginx 编译所需的 lib 库 1 yum -y install make zlib zlib -devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
查看 pcre(正则库)版本
进入编译目录
从官网 下载最新的 nginx(stable version 稳定版) 1 wget http:// nginx.org/download/ nginx-1.24 .0 .tar.gz
解压 nginx 压缩包 1 tar -zxvf nginx-1 .24 .0 .tar.gz
进入解压目录
运行配置脚本 --prefix
参数指定nginx
安装的目录,默认安装在/usr/local/nginx
1 ./configure --prefix=/u sr/local/ nginx
或添加一些模块安装 添加 https,stream 等模块
1 ./configure --prefix=/usr/ local/nginx --with -http_stub_status_module --with -http_ssl_module --with -stream --with -http_v2_module
编译安装 nginx
将 nginx 执行命令软链接到/usr/bin
1 ln -s /usr/ local/nginx/ sbin/nginx /u sr/bin
启动 nginx
设置开机自启动 1 2 echo "/usr/local/nginx/sbin/nginx" >> /etc/ rc.d/rc.local chmod +x /etc/ rc.d/rc.local
执行 nginx -h 查看相关命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 nginx -h nginx version: nginx/1.24 .0 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/ local/nginx/ ) -c filename : set configuration file (default: conf/nginx.conf) -g directives : set global directives out of configuration file
查看 nginx 安装目录
vim语法高亮 1 cp -r /usr/ local/src/ nginx-1.24 .0 /contrib/ vim/* /u sr/share/ vim/vimfiles
自定义conf
目录,例如/usr/local/nginx/conf.d
1 2 3 4 vim /usr/ share/vim/ vimfiles/ftdetect/ nginx.vim au BufRead,BufNewFile */usr/ local/nginx/ conf.d/* set ft=nginx
nginx常用配置
1 2 3 if ($http_user_agent ~* '(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Opera)') { rewrite ^.+ https://m.test.com/$uri; }