文章目录
一、使用docker安装nginx
欲先学习nginx,先要知道nginx的安装方式,点击此处学习怎么在docker中安装nginx!
安装完成之后目录如图
二、nginx中主要的配置文件
nginx配置文件内容结构
nginx.conf
#运行用户,默认即是nginx,可以不进行设置
user nginx;
#Nginx进程,一般设置为和CPU核数一样
worker_processes 1;
#错误日志存放目录
error_log /var/log/nginx/error.log warn;
#进程pid存放位置
pid /var/run/nginx.pid;
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept off; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; # 单个后台进程的最大并发数
}
http {
include /etc/nginx/mime.types; #文件扩展名与类型映射表
default_type application/octet-stream; #默认文件类型
#access_log off; #取消服务日志
#设置日志模式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';#自定义格式
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
access_log /var/log/nginx/access.log main; #nginx访问日志存放位置
sendfile on; #开启高效传输模式
#tcp_nopush on; #减少网络报文段的数量
keepalive_timeout 65; #保持连接的时间,也叫超时时间
#gzip on; #开启gzip压缩
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
}
server块
server {
listen 80; #配置监听端口
server_name localhost; #配置地址
keepalive_requests 120; #单连接请求上限次数。
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #服务默认启动目录
index index.html index.htm; #默认访问文件
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
#error_page 404 /404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启
location = /50x.html {
root /usr/share/nginx/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;
#}
}
三,权限配置(allow和deny)
简单的说,就是我想让谁能访问我的服务器,谁禁止访问我的浏览器
关键词是 ‘allow’和’deny’
顾名思义,allow就是允许谁访问,deny就是禁止谁访问
首先看看自己的ip地址,通过这个网址来获取
我的电脑ip地址是’192.168.170.1’,那么我来禁止我的ip访问服务器
location / {
root /usr/share/nginx/html;
index index.html index.htm;
deny 192.168.170.1;
}
重启nginx之后,访问ip地址首页,结果如下(使用我本机的ip地址(192.168.170.1)----->访问虚拟机上nginx的IP地址(192.168.170.130))
还可以更精准的定位某个路径下不可访问,如下设置
location =/admin {
deny all
}
四、反向代理
在浏览器访问localhost这个域名的时候,展示的页面是tomcat的页面,如下图所示
server{
listen 80; #配置监听端口
server_name localhost; #配置域名
location / {
proxy_pass http://192.168.170.130:8080; #反向代理转发路径
}
}
五,动静分离
动静分离的原理很简单,通过location对请求url进行匹配即可,在/usr/share/nginx/html(此目录是docker中nginx的目录不是下方挂载出来的目录)下创建 /static/index再分别创建css img js配置如下:
####静态资源访问
location /static/ {
root /usr/share/nginx/html; #页面中的src等路径直接匹配到nginx的路径里
# 这里的路径是docker中nginx里面的真实路径,不是挂载出来的路径
}
####动态资源访问
location / {
proxy_set_header Host $host;
proxy_pass http://gulimall;
}
注意此处的root和alias的区别
root的处理结果是: root路径 + location路径
alias的处理结果是:使用alias路径替换location路径
location /img/ {
alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。
六,location 匹配规则
https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx/nginx_local_pcre.html
七,参考文章和视频
https://www.cnblogs.com/shihaiming/p/6183923.html
https://www.cnblogs.com/haoworld/p/nginx-shi-xian-dong-jing-fen-li.html
https://www.bilibili.com/video/BV1np4y1C7Yf?from=search&seid=14360754235817037496