nginx是用于 Web 服务、反向代理、内容缓存、负载均衡、媒体流传输等场景的开源软件。
主要作用有三个:1、反向代理
- 负载均衡
- 动静分离
下载地址:nginx: download
nginx执行命令及启动
//假设安装在E:\server\nginx-1.20.0目录下
//cmd命令进入安装文件
//启动
E:\server\nginx-1.20.0>start nginx
//或
E:\server\nginx-1.20.0>nginx.exe
//注意:建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作。
//停止
E:\server\nginx-1.20.0>nginx -s stop
或
E:\server\nginx-1.20.0>nginx -s quit
//注意:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息
//重启nginx
E:\server\nginx-1.20.0>nginx -s reload
//当配置信息修改,可以使用此命令重启nginx
//重新打开日志文件
E:\server\nginx-1.20.0>nginx -s reopen
//查看nginx版本
E:\server\nginx-1.20.0>nginx -v
//查看所有可执行的命令
E:\server\nginx-1.20.0>nginx -h
浏览器访问,出现如下页面表示nginx启动成功:
注意:如果命令行停止不了nginx,进入到电脑->任务管理器->进程(选中nginx右键鼠标结束任务)
nginx 代理、负载均衡和本地图片配置
# 全局配置
#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 配置
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;
# 负载均衡配置 - weight 数值越大防范概率越大
upstream zdy {
# 不可用服务器在30s内与服务端通讯成功2次,则认为服务器恢复
server 127.0.0.1:8166 max_fails=2 fail_timeout=30s weight=1;
server 127.0.0.1:8177 max_fails=2 fail_timeout=30s weight=2;
}
server {
listen 81;
server_name 127.0.0.1;
# 项目访问负载均衡调用相应的端口
location / {
root html;
index index.html index.htm;
# 反向代理
proxy_pass http://zdy;
}
#配置访问images目录下的图片
#图片访问地址:http://localhost:81/images/demo_1.jpg
#前提本地D盘存在static文件夹,且里面存在demo_1.jpg图片
location /images/ {
alias D:/static/;
autoindex on; # 可选,允许目录列表
access_log off; # 可选,禁用访问日志记录
expires 30d; # 设置缓存时间
}
# 默认
#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 { # https 配置
# 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;
# }
#}
}