1.Nginx适用环境:
实现网站服务 web
反向代理服务器
可以为web服务器做代理
或者是mail服务器做代理(很少)
特点:
1、隐藏后端地址
2、缓存、可以加快客户端的访问速度
3、负载均衡
4、承载大并发
2.Nginx安装
nginx下载地址
http://nginx.org/
https://www.nginx.cn/
https://www.nginx.cn/doc/
安装前准备:
[root@nginx ~]# useradd nginx #不创建的话默认用户nobody
[root@nginx ~]# mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
#client : 存客户端文件
#proxy:代理文件
#fastcgi:cgi是通用网关协议,不高效,fastcgi是一种常驻型CGI服务,
#uwsgi:以下两种类似于fastcgi,不同的传输协议而已
#scgi:
[root@localhost ~]# yum install -y gcc-c++ pcre-devel openssl openssl-devel zlib-devel #依赖环境
[root@nginx ~]# tar zxf nginx-1.20.0.tar.gz
[root@nginx ~]# cd nginx-1.20.0/
[root@nginx nginx-1.11.4]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--with-file-aio \
--with-http_secure_link_module \
--with-threads \
make && make install
如果需要删除源码包,直接删除安装目录即可。
[root@localhost ~]# rm -rf /usr/local/nginx/
3.nginx目录介绍
/usr/local/nginx/conf nginx.conf 配置文件
/usr/local/nginx/html 默认网页目录
/usr/local/nginx/logs 日志(启动后产生)
/usr/local/nginx/sbin nginx二进制程序(启动关闭、配置文件读取、语法检查等功能)
启动nginx
[root@nginx ~]# /usr/local/nginx/sbin/nginx
[root@nginx ~]# ss -antp | grep nginx
LISTEN 0 128 *:80 *:* users:(("nginx",pid=7691,fd=6),("nginx",pid=7690,fd=6))
[root@nginx ~]# ps aux | grep nginx
root 7690 0.0 0.1 47552 1116 ? Ss 11:20 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 7691 0.0 0.2 50076 2220 ? S 11:20 0:00 nginx: worker process
root 7708 0.0 0.0 112660 932 pts/0 S+ 11:21 0:00 grep --color=auto nginx
4.设置开机自启nginx
[root@nginx ~]# tail -1 /etc/rc.d/rc.local
/usr/local/nginx/sbin/nginx
[root@nginx ~]# chmod a+x /etc/rc.d/rc.local
5.nginx常用命令
mkdir /root/bin
ln -s /usr/local/nginx/sbin/nginx /root/bin/nginx
做完以上步骤后,可以在任意路径下使用nginx命令
常用选项:
nginx -t
nginx
nginx -s reload
nginx -s stop