一、介绍
Nginx 是一个高性能的 HTTP 和反向代理 Web 服务器,由俄罗斯的伊戈尔·赛索耶夫开发,第一个版本发布于 2004 年 10 月 4 日。
二、特点
1.内存占用少
2.并发能力强
3.配置简单方便
4.bug非常少
5.安装简单
6.服务运行稳定
三、作用
反向代理:客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器 IP
地址。正向代理是代理客户端的,让你能正常访问目的服务器。反向代理是代理服务器的,让大量的请求均衡地访问到某一台服务器上。
Nginx 内置了轮询和加权轮询来达到负载均衡的目的,各个服务器都可以设置自己的权重,配置好的服务器设置权重大一些可以最大使用效率。
动静分离:我们项目中有些静态文件不需要进行后台的访问,我们可以把动态资源和静态资源分开,通过Nginx把请求分开,静态资源的请求就不需要经过 Web 服务器处理了,从而提高整体上的资源的响应速度。
四、安装
针对不同的操作系统,Nginx 的安装各不相同。Windows 可以直接到官网下载 zip 绿色安装包,解压后就可以了。
http://nginx.org/en/download.html
我们下载好后,解压。
打开conf目录,找到nginx.conf文件,这个文件就是nginx的配置文件
五、常用命令
start nginx 启动nginx
nginx -s stop 停止nginx
nginx -s quit 安全退出nginx
nginx -s reload 重新加载配置文件
ps aux|grep nginx 查看nginx进程
我们打开cmd,并启动nginx
由于当前的端口默认是80,所以我们可以通过http://localhost/访问默认的地址
六、Nginx的配置
我们打开nginx.conf这个文件,查看Nginx的相关配置。
#user nobody;
worker_processes 1; #nginx进程数,一般设置和cpu核心数一样
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024; #每个进程允许最大并发数
}
http {
include mime.types; #文件扩展名与类型映射表
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on; #默认开启高效传输模式
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #保持连接的时间,也称为超时时间,单位是秒
#gzip on;
server {
listen 80; #配置端口
server_name localhost; #配置的域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html; #网站根目录
index index.html index.htm; #默认首页文件
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #默认50x对应的访问页面
location = /50x.html {
root html; #50x页面的目录
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
我们修改下端口
并执行nginx -s reload命令,重新加载配置
然后我们使用新地址访问测试地址http://localhost:8080/
发现我们的设置的端口,起作用了。
七、反向代理如何配置
实现效果:使用 Nginx
反向代理,访问www.123.com
直接跳转到127.0.0.1:8080
注意:此处如果要想从www.123.com
跳转到本机指定的ip
,需要修改本机的hosts
文件。此处略过
配置代码
如上配置,Nginx
监听 8080
端口,访问域名为www.123.com:8080
(不加端口号时默认为 80
端口),故访问该域名时会跳转到 127.0.0.1:8080
路径上。
八、Nginx做为Windows服务
1.下载WinSW.NET4.exe,下载地址为: https://github.com/winsw/winsw/releases/tag/v2.11.0
2.将其放在 Nginx解压的目录下并重命名为nginx-service.exe
3.在nginx目录下新增文件nginx-service.xml,内容为:
<service>
<!-- 唯一服务ID-->
<id>nginx</id>
<!-- 显示服务的名称 -->
<name>Nginx Service</name>
<!-- 服务描述 -->
<description>Nginx服务</description>
<!-- 日志路径 -->
<logpath>E:\nginx\nginx-1.24.0\logs\</logpath>
<!-- 日志模式 -->
<logmode>roll</logmode>
<!-- 可执行文件的命令 -->
<executable>E:\nginx\nginx-1.24.0\nginx.exe</executable>
<!-- 停止可执行文件的命令 -->
<stopexecutable>E:\nginx\nginx-1.24.0\nginx.exe -s stop</stopexecutable>
</service>
里面的路径统一切换到自己电脑的nginx路径。
4.以管理员身份打开cmd,在nginx目录下输入:nginx-service.exe install,执行完成后nginx就增加到windows服务中,服务名称为Nginx Service。
我们在服务列表中发现了我们新增的Nginx Service服务
我们右键启动这个服务,再次访问测试地址成功。
卸载命令:nginx-service.exe uninstall
服务卸载成功。