京东6.18大促主会场领京享红包更优惠

 找回密码
 立即注册

QQ登录

只需一步,快速开始

docker启动nginx及常见nginx设置方式

2024-11-3 14:55| 发布者: 44f6fa4f5| 查看: 66| 评论: 0

摘要: 目录nginx简单启动nginx相干目录nginx紧张命令挂载文件启动设置文件剖析常用三个设置详细剖析locationifrewrite常用设置反向署理后端apireact 项目设置服务于爬虫的页面TDK总结nginx 为方便在设置堕落时快速规复,使
目录

nginx

为方便在设置堕落时快速规复,使用docker方式来设置nginx,下文所有命令针对docker方式。

简单启动

[code]sudo docker run --name nginx_3000 -p 3000:80 -d nginx[/code]

可以在3000端口访问nginx,如下图:

nginx相干目录

实际使用过程中,我们需要对nginx的转发规则等选项举行设置。docker版本的各个设置文件默认所在路径为:

  • 网页root目录:/usr/share/nginx/html
  • nginx设置文件:/etc/nginx/nginx.conf
  • 日记文件目录:/var/log/nginx

nginx紧张命令

[code]nginx stop #快速关机 nginx quit #优雅的关机 nginx reload #重新加载设置文件 nginx reopen #重新打开日记文件 nginx -t #查抄设置,同时也是输出设置文件所在位置 nginx -s reload #修改设置后重载[/code]

挂载文件启动

docker启动nginx后每次修改设置文件都得实行

[code]docker exec nginx_3000 /bin/bash[/code]

进入容器的shell后举行操纵,比力繁琐,而且用vi编辑器修改内容多时不太方便,以是可以将网页文件及设置文件从宿主机中挂载到nginx中,如答应以方便在在宿主机中利用vscode举行修改。

起首先在宿主机中某一位置建立网页、日记和设置目录

[code]mkdir html mkdir logs mkdir conf[/code]

拷贝容器中nginx默认设置到宿主机刚才新建的conf目录中

[code]docker cp nginx_3000:/etc/nginx/nginx.conf ./conf/[/code]

删掉刚才建立的nginx容器,重新建立带目录映射的容器,

[code]docker run -d -p 80:80 --name nginx_zf -v ~/docker_file/nginx/html:/usr/share/nginx/html -v ~/docker_file/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/docker_file/nginx/logs:/var/log/nginx nginx[/code]

此中各个设置项含义如下:

  • -d 后台运行
  • -p 端口映射 3000宿主机端口 80 docker监听端口
  • –name 指定容器名称
  • -v 目录映射 前者是宿主机路径 后者是docker容器路径 详细路径根据自己环境设定
  • html为网页内容
  • conf 为nginx设置
  • logs 为 日记

启动乐成后,我们就可以在宿主机中修改nginx的设置文件了,一样平常修改完成后,可以restart容器来见效设置,大概进入nginx容器,实行重载设置。

默认nginx设置文件内容[code]/etc/nginx/nginx.conf[/code]如下:

[code]user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }[/code]

留意文件中又引入了 [code]/etc/nginx/conf.d/*.conf[/code]文件,如下:

[code]server { listen 80; listen [::]:80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }[/code]

为简化操纵,可以直接修改宿主机中的 [code]nginx.conf[/code]文件,不导入其他文件,如今以一个url转发为例修改宿主机的设置文件如下:

[code]worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; autoindex on;# 消除403报错 location / { root /usr/share/nginx/html; index index.html index.htm; } # 访问 localhost:端口号/test 转发到 www.baidu.com location /test { proxy_pass http://www.baidu.com; } } }[/code]

假如启动后报403 可以在location上加上 autoindex on; 设置

设置文件剖析

紧张结构

[code]... #全局块 events { #events块 ... } http #http块 { ... #http全局块 server #server块 { ... #server全局块 location [PATTERN] #location块 { ... } location [PATTERN] { ... } } server { ... } ... #http全局块 }[/code]
  • 1、全局块:设置影响nginx全局的指令。一样平常有运行nginx服务器的用户组,nginx进程pid存放路径,日记存放路径,设置文件引入,答应天生worker process数等。
  • 2、events块:设置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模子处置惩罚连接请求,是否答应同时接受多个网路连接,开启多个网络连接序列化等。
  • 3、http块:可以嵌套多个server,设置署理,缓存,日记定义等绝大多数功能和第三方模块的设置。如文件引入,mime-type定义,日记自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
  • 4、server块:设置虚拟主机的相干参数,一个http中可以有多个server。
  • 5、location块:设置请求的路由,以及各种页面的处置惩罚环境。紧张修改都集中在这一块。

举例子:

[code]########### 每个指令必须有分号竣事。################# #user administrator administrators; #设置用户大概组,默认为nobody nobody。 #worker_processes 2; #答应天生的进程数,默认为1 #pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址 error_log log/error.log debug; #订定日记路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg events { accept_mutex on; #设置网路连接序列化,防止惊群征象发生,默认为on multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off #use epoll; #事件驱动模子,select|poll|kqueue|epoll|resig|/dev/poll|eventport worker_connections 1024; #最大连接数,默认为512 } http { include mime.types; #文件扩展名与文件范例映射表 default_type application/octet-stream; #默认文件范例,默认为text/plain #access_log off; #取消服务日记 log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式 access_log log/access.log myFormat; #combined为日记格式的默认值 sendfile on; #答应sendfile方式传输文件,默认为off,可以在http块,server块,location块。 sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。 keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。 upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333 backup; #热备 } error_page 404 https://www.baidu.com; #错误页 server { keepalive_requests 120; #单连接请求上限次数。 listen 4545; #监听端口 server_name 127.0.0.1; #监听地址 location ~*^.+$ { #请求的url过滤,正则匹配,~为区分巨细写,~*为不区分巨细写。 #root path; #根目录 #index vv.txt; #设置默认页 proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表 deny 127.0.0.1; #拒绝的ip allow 172.18.5.54; #答应的ip } } }[/code]

常用三个设置详细剖析

location

[code]location [=|~|~*|^~|@] /uri/ { ... } [/code]
  • = : 表现准确匹配背面的url
  • ~ : 表现正则匹配,但是区分巨细写
  • ~* : 正则匹配,不区分巨细写
  • ^~ : 表现普通字符匹配,假如该选项匹配,只匹配该选项,不匹配别的选项,一样平常用来匹配目录
  • @ : “@” 定义一个命名的 location,使用在内部定向时,例如 error_page

上述匹配规则的优先匹配顺序:

  • = 前缀的指令严格匹配这个查询。假如找到,停止搜索;
  • 所有剩下的常规字符串,最长的匹配。假如这个匹配使用 ^~ 前缀,搜索停止;
  • 正则表达式,在设置文件中定义的顺序;
  • 假如第 3 条规则产生匹配的话,结果被使用。否则,使用第 2 条规则的结果。

if

用法:if (条件判定) { 详细的rewrite规则 }

  • 条件判定语句由Nginx内置变量、逻辑判定符号和目标字符串三部分组成。
  • rewite规则见下方 [code]rewrite[/code] 部分

常见逻辑判定符号:

  1. = 等于
  2. != 不等于
  3. ~ 匹配 区分巨细写
  4. ~* 匹配 不区分巨细写
  5. !~ 不匹配 区分巨细写
  6. !~* 不匹配 不区分巨细写

!表现相反的意思,为匹配符号,它右侧为正则表达式,区分巨细写,而*为不区分巨细写匹配。

目标字符串可以是正则表达式,通常不消加引号,但表达式中有特殊符号时,比如空格、花括号、分号等,需要用单引号引起来。

常见Nginx内置变量:

  • 1.$remote_addr 与 $http_x_forwarded_for 用以记录客户端的ip地址;
  • 2.$remote_user :用来记录客户端用户名称;
  • 3.$time_local : 用来记录访问时间与时区;
  • 4.$request : 用来记录请求的url与http协议;
  • 5.$status : 用来记录请求状态;乐成是200;
  • 6.$body_bytes_s ent :记录发送给客户端文件主体内容巨细;
  • 7.$http_referer :用来记录从谁人页面链接访问过来的;
  • 8.$http_user_agent :记录客户端浏览器的相干信息;

例子:

[code]if ($http_user_agent ~ MSIE) //user_agent带有MSIE字符的请求,直接返回403状态码 ,MSIE ->IE浏览器 { return 403; } if ($request_method = POST) //当请求的方法为POST时,直接返回405状态码 { return 405; //在该示例中并未用到rewrite规则,if中支持用return指令,直接返回状态码 } if(!-f $request_filename) //当请求的文件不存在,将会实行下面的rewrite规则,-f判定是否为文件,!-f判定这个文件是否存在 { rewrite 语句; #紧张 }[/code]

rewrite

用法: rewrite [正则] [更换] 标志位

rewrite是实现URL重写的关键指令,根据regex(正则表达式)部分内容,重定向到replacement,末端是flag标志位

正则表达式匹配,与[code]if[/code]逻辑判定雷同,如下:

  1. ~ 为区分巨细写匹配
  2. ~* 为不区分巨细写匹配
  3. !和!*分别为区分巨细写不匹配及不区分巨细写不匹配

rewrite除正则匹配外,还支持对文件目录判定,有以下几种:

  1. -f和!-f用来判定是否存在文件
  2. -d和!-d用来判定是否存在目录
  3. -e和!-e用来判定是否存在文件或目录
  4. -x和!-x用来判定文件是否可实行

flag标志分为下面4种:

  1. last :本条规则匹配完成后,继承向下匹配新的location URI规则
  2. break :本条规则匹配完成即停止,不再匹配背面的任何规则
  3. redirect :返回302临时重定向,浏览器地址会显示跳转后的URL地址
  4. permanent :返回301永世重定向,浏览器地址栏会显示跳转后的URL地址

使用last和break实现URI重写,浏览器地址栏稳定。而且两者有细微差别,使用alias指令必须用last标志;使用proxy_pass指令时,需要使用break标志。

Last标志在本条rewrite规则实行完毕后,会对其所在server{…}标签重新发起请求,而break标志则在本条规则匹配完成后,停止匹配。

常用设置

反向署理后端api

[code]worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 10240; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; #后端多实例集群,可根据权重分配请求 upstream apiaddr{ server 192.168.31.101:8080 weight=5; #weight 为权重 server 192.168.31.102:8080 weight=3; } server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; autoindex on;# 消除403报错 location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri /index.html;#加上该设置项,使能在react项目中手动在浏览器url栏手动输入url。 } # 反向署理设置 location /api/{ proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://apiaddr/; #多实例 利用upstream来转发 #proxy_pass http://192.168.31.101:8080/; #单实例直接指定后端api地址 #根据是否有跨域需求修改 add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,DELETE,OPTIONS; #也可以单独 附加请求头,根据需要修改 #proxy_set_header your-custome-header "myHeader"; #proxy_set_header X-NginX-Proxy true; } } } [/code]

react 项目设置服务于爬虫的页面TDK

[code]user nginx; worker_processes auto; #error_log /var/log/nginx/error.log warn; #修改 日记输出等级 共同下方 rewrite_log on; 可以在errlog中检察rewrite转发日记,方便调试 error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; gzip on; server { listen 80;#HTTP 的端口 server_name localhost; #域名 charset utf-8; #access_log logs/host.access.log main; #此处是开启rewrite日记,而且需要把nginx的错误日记级别改为notice rewrite_log on; # 前端页面 location / { root /usr/share/nginx/html; index index.html; try_files $uri /index.html;#加上了这个,就可以在url背面直接加路由上设置path了。 } # 多个预老师成的 供爬取的 html页面 # 1.商城首页 /front/shoppingIndex # 2.资助中央 /front/help # 3.商品列表 /front/goodsList # 4.直接采购 /front/directbuy # 5.优质供应商 /front/highQualitySupplier # 6.快速采购 /front/rapidbuy location ~ /front/(shoppingIndex|help|goodsList|directbuy|highQualitySupplier|rapidbuy)/?$ { #?表现0个大概1个/ root /usr/share/nginx/html; index index.html; try_files $uri /index.html;#加上了这个,就可以在url背面直接加路由上设置path了。 #设置一个 判定是否为爬虫的 变量 set $isspider no; #默认不是爬虫 #爬虫判定(谷歌、百度、360、微软) if ($http_user_agent ~* .*(Googlebot|Baiduspider|360Spider|bingbot).*){ set $isspider yes; #是爬虫 } #假如是爬虫的话 则重写到 front 文件夹中 的对应静态html if ($isspider = yes) { rewrite ^(.*)$ $1.html break; #permanent; #rewrite ^(.*)$ /prerender/index3.html break; } } error_page 404 /html/404.html; # redirect server error pages to the static page /50x.html # error_page 502 503 /html/502.html; error_page 500 504 /50x.html; location = /50x.html { root html; } } } [/code]

总结

以上为个人履历,希望能给大家一个参考,也希望大家多多支持脚本之家。


来源:https://www.jb51.net/server/3263327ak.htm
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
关闭

站长推荐上一条 /6 下一条

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2025-7-1 18:55 , Processed in 0.031283 second(s), 18 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2025 Discuz! Team.

返回顶部