文件路径 cd /usr/local/nginx/sbin/
基本命令
./nginx 启动
./nginx -s stop 停止
./nginx -s quit 安全退出
./nginx -s reload 重新加载配置文件
ps aux|grep nginx 查看nginx进程
配置文件详解
分为三大部分
1. 全局块
worker_processes 1;
:并发处理服务的关键配置
值越大,可以支持的并发处理量也越大,受硬件软件等设备的制约
2. events快
events {
worker_connections 1024; # 支持的最大连接数
}
3. 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;
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;
# }
#}
}
配置最频繁的部分
http块包括http全局块和server块
a. http全局块
包含文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单链接请求数上限
b. server块
server {
listen 80; # 监听端口
server_name localhost; # 主机名称
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #
root html;
index index.html index.htm;
}
}
反向代理实例1
通过访问www.123.com 访问nginx 然后代理到本地tomcat8080中去
1. 配置windows本地host文件
进行域名和ip的相关配置
然后访问www.123.com:8080 也可以访问到tomcat
2. 修改nginx的配置文件
localhost改成我们的ip: 192.168.153.130
添加这一句 这样我们访问nginx的80端口的/路径时,就会代理到127.0.0.1:8080
3. 启动nginx 最终测试
最终效果:我们访问www.123.com 访问的是本地的tomcat
反向代理实例2
监听9001端口,根据访问的路径跳转到不同端口的服务器中
访问192.168.153.130:9001/edu 直接跳转到127.0.0.1:8080
访问192.168.153.130:9001/stu 直接跳转到127.0.0.1:8081
也就是nginx对外只暴露9001,用户不知道访问的是8080还是8081,反向代理
1. 自己开启多个tomcat (8080/8081)
配置一下8081端口
1、8005改成8015
2、8080改成8081
防火墙放行8081端口之后重启防火墙就可以访问了
2. 在8080的webapps下创建目录src,并放入文件a.html
成功访问到a.html
同理,在8081下的webapps下的stu中也放入b.html 并成功访问
3. nginx配置文件,进行反向代理配置
就用已有的注释的来修改
修改后:
~ /edu/
:这是正则表达式,意思是只要路径中有edu就跳转
小知识:
=:不含正则表达式 严格匹配
~:正则表达式 区分大小写
~*:不区分大小写
^~:不含正则表达式 。。。
4. 重新加载下nginx
./nginx -s stop
./nginx
5. 最终测试
访问9001端口,被nginx监听到了,进入nginx
访问/edu路径,被9001端口location监听到了,访问8080服务器
负载均衡
访问http://192.168.153.130/edu/a.html,实现负载均衡
按原来的逻辑,会一直访问8080的页面
准备工作:
8080中 /edu/a.html
8081中也要创建 /edu/a.html
负载均衡配置
然后重新启动nginx
最后可以发现,一次走8080服务器,一次走8081服务器,实现了负载均衡
分配策略
- 轮询(默认)
按时间顺序逐一分配upstream myserver{ server 192.168.153.130:8080; server 192.168.153.130:8081; }
- weight权重
权重默认为1upstream myserver{ server 192.168.153.130:8080 weight=5; server 192.168.153.130:8081 weight=10; }
- ip_hash
根据请求ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session问题upstream myserver{ ip_hash; server 192.168.153.130:8080; server 192.168.153.130:8081; }
- fair(第三方)
按后端服务器的响应时间分配请求,响应时间短的优先分配upstream myserver{ server 192.168.153.130:8080; server 192.168.153.130:8081; fair; } ```
动静分离
静态资源不通过Tomcat,直接通过Nginx获取
准备工作
创建一个根目录下文件夹data,里面再创建两个文件夹www和image,分别往里面放一个html和一张图片
具体配置
测试
autoindex on
列出文件路径
成功获取到静态html和图片