一、什么是Nginx
Nginx(发音为"engine X")是一个开源的高性能、轻量级的 Web 服务器和反向代理服务器。它以其出色的性能、稳定性和可扩展性而广受欢迎,被用于构建高流量的网站、负载均衡、反向代理、缓存以及作为应用服务器的前端。
二、Windows安装
2.1 官网
2.2 点击download
2.3 选择稳定版
2.4 解压
2.5 测试
2.5.1 nginx.exe启动
双击nginx目录下的nginx.exe,双击后一个黑色的弹窗一闪而过就消失了,启动就完成了。
2.5.2 查看是否启动成功
浏览器地址栏输入 http://localhost,出现以下页面说明启动成功
三、配置文件
3.1 位置
安装目录下conf/nginx.conf(修改过记得重启nginx服务)
3.2 三部分
3.2.1 全局块
3.2.2 events 块
3.2.3 http 块
Nginx 服务器配置中最频繁的部分
3.2.3.1 http 全局块
3.2.3.2 server 块
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置
3.2.3.2.1 全局 server 块
3.2.3.2.2 location 块
四、反向代理
我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器 IP 地址。
五、配置
5.1 nginx.conf
#user nobody;
worker_processes 1;
#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;
location = /50x.html {
root html;
}
# 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;
# }
#}
}
5.2 外部配置文件
5.2.1 server块外部配置
nginx/conf目录下创建一个serverConfig文件夹,然后创建一个server.conf配置文件
5.2.2 配置server.conf
server {
listen 81; # 监听端口
server_name www.first.com; # 监听域名或IP
location / {
proxy_pass http://127.0.0.1:3000; # 代理转发的地址
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
5.2.3 修改nginx.conf,引用外部配置
# 引入外部配置文件
include serverConfig/*.conf;
六、本地
映射网址添加到本地的C:\Windows\System32\drivers\etc\hosts文件中
127.0.0.1 www.first.com # 表示将这个网址映射到本地
七、启停
7.1 停
cmd命令窗口输入nginx命令(快速停止nginx)
nginx -s stop
7.2 启
双击nginx目录下的nginx.exe
八、测试
浏览器访问 http://www.first.com:81
总结
1.启动nginx,通过配置生成对应监听
2.客户端发送请求
3.优先本地解析域名 ,得到ip
4.ip:端口发送请求到目标服务器
5.目标服务器nginx一直在监听 ,先通过ip再通过域名再通过端口匹配,匹配不到走default_server,没有显示定义default_server第一个server为隐式的default server
7.反向代理规则代理到配置地址
8.配置地址服务器一直在监听配置端口,得到请求并处理,响应给nginx
9.nginx将结果返给客户端