nginx的作用
反向代理
Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理
负载均衡
Nginx提供的负载均衡策略有两种:内置策略和扩展策略。内置策略为轮询,加权轮询,lp hash。扩展策略,就天马行空,只有你想不到的没有他做不到的。
动静分离
动静分离,在我们的软件开发中,有些请求是需要后台处理的,有些请求是不需要经过后台处理的(如:css、html、jpg、js等等文件),这些不需要经过后台处理的文件成为静态文件。让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了 拆分以后,我们就可以根据静态资源的特点将其做缓存操作。提高资源相应的速度。
目前,通过使用Nginx大大提高了我们网站的影响速度,优化了用户体验。
nginx安装
nginx官网
http://nginx.org/en/download.html
window安装nginx
安装好直接解压即可
java\nginx-1.18.0 双击nginx.exe 或者 cmd输入nginx.exe启动
看到这个则启动成功
linux安装nginx
安装linux的稳定版本
执行:./configure
./configure \
--prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/run \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-http_image_filter_module \
--with-debug \
执行make
**如果报:make 没有规则可以创建“default”需要的目标“build”。 停止。
执行如下代码再make:
yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel
执行:make install
查看是否安装成功: whereis nginx
进入nginx:cd /usr/local/nginx/sbin
启动nginx: ./nginx
可看到启动成功:
nginx常用命令
-
cd /usr/local/nginx/sbin/
-
./nginx 启动
-
./nginx -s stop 停止
-
./nginx -s quit 安全退出
-
./nginx -s reload 重新加载配置文件 重启
-
ps aux|grep nginx 查看nginx进程
nginx.conf配置
- nginx.conf的结构
#全局配置
events {
worker_connections 1024;
}
http {
http配置
upstream xx{
//负载均衡配置
}
upstream hntianshu { //负载均衡
# 服务器资源 server weight权重
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight =1;
}
server {
listen 80;
server_name localhost;
//代理
//配置www.hntiansu.cn
localtion /{
// xxxx 123.xxx(服务器)
proxy_pass http://www.hntianshu.cn 如果是localhost:80 则跳转到www.hntianshu.cn的网站
}
//配置www.hntiansu.cn/admin
localtion /{
// xxxx 456.xxx
}
}
server {
listen 443;
server_name localhost;
//代理
}
}
安装流程2
-
1.去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本
-
2.上传nginx到linux系统
-
3.安装依赖环境
(1)安装gcc环境yum install gcc-c++
(2)安装PCRE库,用于解析正则表达式
``` yum install -y pcre pcre-devel ```
(3)zlib压缩和解压缩依赖,
yum install -y zlib zlib-devel
(4)SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https
yum install -y openssl openssl-devel
-
4.解压,需要注意,解压后得到的是源码,源码需要编译后才能安装
tar -zxvf nginx-1.16.1.tar.gz
-
5.编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错
mkdir /var/temp/nginx -p
-
6.在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件
./configure \n --prefix=/usr/local/nginx \n --pid-path=/var/run/nginx/nginx.pid \n --lock-path=/var/lock/nginx.lock \n --error-log-path=/var/log/nginx/error.log \n --http-log-path=/var/log/nginx/access.log \n --with-http_gzip_static_module \n --http-client-body-temp-path=/var/temp/nginx/client \n --http-proxy-temp-path=/var/temp/nginx/proxy \n --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \n --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \n --http-scgi-temp-path=/var/temp/nginx/scgi
- 注: 代表在命令行中换行,用于提高可读性
- 配置命令:
-
7.make编译
make
-
8.安装
make install
-
9.进入sbin目录启动nginx
./nginx
- 停止:./nginx -s stop
- 重新加载:./nginx -s reload
-
10.打开浏览器,访问虚拟机所处内网ip即可打开nginx默认页面,显示如下便表示安装成功:
注意事项:
1. 如果在云服务器安装,需要开启默认的nginx端口:80
2. 如果在虚拟机安装,需要关闭防火墙
3. 本地win或mac需要关闭防火