赞
踩
Nginx(发音为"engine X")是一个开源的高性能、轻量级的 Web 服务器和反向代理服务器。它以其出色的性能、稳定性和可扩展性而广受欢迎,被用于构建高流量的网站、负载均衡、反向代理、缓存以及作为应用服务器的前端。
双击nginx目录下的nginx.exe,双击后一个黑色的弹窗一闪而过就消失了,启动就完成了。
浏览器地址栏输入 http://localhost,出现以下页面说明启动成功
安装目录下conf/nginx.conf(修改过记得重启nginx服务)
Nginx 服务器配置中最频繁的部分
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置
我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器 IP 地址。
#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; # } #} }
nginx/conf目录下创建一个serverConfig文件夹,然后创建一个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;
}
}
# 引入外部配置文件
include serverConfig/*.conf;
映射网址添加到本地的C:\Windows\System32\drivers\etc\hosts文件中
127.0.0.1 www.first.com # 表示将这个网址映射到本地
cmd命令窗口输入nginx命令(快速停止nginx)
nginx -s stop
双击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将结果返给客户端
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。