简述:
Nginx是一个开源的Web服务器,同时Nginx也提供了反向代理和负载均衡的功能。
Nginx通常作为负载均衡器暴露在外网接受用户请求,同时也使用其反向代理的功能,将用户的请求转发到实际提供服务的内网服务器。
Windows10需要什么环境下需要安装Nginx
把windows开发机作为服务对外提供服务,只是个人在本地测试或者没有应用于商业用途的情况下,这时适合在windows装上Nginx服务器。
安装:
下载压缩包:
http://nginx.org/en/download.html
1.15.2版本:http://nginx.org/download/nginx-1.15.2.zip
下载完成后解压到自己想放置的位置,在这里我把它解压到D:\Nginx(在目录的路径中不允许含有中文字段,也不建议有空格)
启动Nginx服务器:
有很多种方法启动nginx
(1)直接双击nginx.exe,双击后一个黑色的弹窗一闪而过
(2)打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe 或者 start nginx ,回车即可
验证是否启动成功,在流浪器的地址栏输入http://localhost:80,回车,出现以下页面说明启动成功
ginx的配置文件是conf目录下的nginx.conf,默认配置的nginx监听的端口为80,如果80端口被占用可以修改为未被占用的端口。
#user nobody;
#nginx进程,一般设置为和cpu核数一样
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中调整server节点就可以了,
检查80端口是否被占用的命令是: netstat -ano | findstr 0.0.0.0:80 或 netstat -ano | findstr "80"
当我们修改了nginx的配置文件nginx.conf 时,不需要关闭nginx后重新启动nginx,只需要执行命令 nginx -s reload 即可让改动生效。
示例我们保留即可,我们配置反向代理、负载均衡直接在后面追加即可,
- 反向代理配置示例
-
server { listen 80; #监听80端口 server_name blog.test.ken.io; #监听的域名 location / { #转发或处理 proxy_pass https://ken.io; } error_page 500 502 503 504 /50x.html;#错误页 location = /50x.html { root /usr/share/nginx/html; } }
- 负载均衡配置示例
- 我们可以修改nginx的配置文件nginx.conf 达到访问nginx代理服务器时跳转到指定服务器的目的,即通过proxy_pass 配置请求转发地址,即当我们依然输入http://localhost:80 时,请求会跳转到我们配置的服务器
-
upstream serverswitch { server 127.0.0.1:8080; } server { listen 80; #监听80端口 server_name ss.test.ken.io; #监听的域名 location / { #转发或处理 proxy_pass https://serverswitch; } error_page 500 502 503 504 /50x.html;#错误页 location / { proxy_pass http://xxx; } }
我们可以配置多个目标服务器,当一台服务器出现故障时,nginx能将请求自动转向另一台服务器
upstream serverswitch { server 127.0.0.1:8080 weight=2; server 127.0.0.1 weigth=1; } server { listen 80; #监听80端口 server_name ss.test.ken.io; #监听的域名 location / { #转发或处理 proxy_pass https://serverswitch; } error_page 500 502 503 504 /50x.html;#错误页 location / { proxy_pass http://xxx; } }
上面还加了一个weight属性,此属性表示各服务器被访问到的权重,weight
越高被访问到的几率越高。
-
nginx配置静态资源
将静态资源(如jpg|png|css|js等)放在如下配置的D:/nginx-1.12.2/static目录下,然后在nginx配置文件中做如下配置(注意:静态资源配置只能放在 location / 中),浏览器中访问 http://localhost:80/1.png 即可访问到 D:/nginx-1.12.2/static目录下的 1.png图片
关闭nginx
如果使用cmd命令窗口启动nginx,关闭cmd窗口是不能结束nginx进程的,可使用两种方法关闭nginx
(1)输入nginx命令 nginx -s stop(快速停止nginx) 或 nginx -s quit(完整有序的停止nginx)
(2)使用taskkill taskkill /f /t /im nginx.exe
命令 | 说明 |
nginx -h | 查看帮助信息 |
nginx -v | 查看Nginx版本 |
nginx -s stop | 停用Nginx |
nginx -s quit | 优雅的停用Nginx |
nginx -s reload | 重新加载配置,并优雅的重启进程 |
nginx -s reopen | 重启日志文件 |