赞
踩
旧版本的nginx 所有配置都放在一个nginx.conf文件中,新版本的nginx 配置文件分为 nginx.cong
,和sites-available
,site-enabled
三个部分,其中sites-enabled
中是对 site-avaliable
中文件的引用。
... #全局块 #配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程 #pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等 events { #events块 ... #配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件 #驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列等。 } http #http块 { ... #http全局块 #可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime- #type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。 server #server块。 { ... #server全局块,配置虚拟主机的相关参数,一个http中可以有多个server location [PATTERN] #location块,配置请求的路由,以及各种页面的处理情况。 { ... } } ... #http全局块 }
user www-data; #配置用户或者组 worker_processes auto;# 允许生成的进程数 pid /run/nginx.pid; #指定nginx进程运行文件存放地址 include /etc/nginx/modules-enabled/*.conf;# 加载模块配置文件(该目录下为软连接) events { worker_connections 768; #单个进程最大连接数 # multi_accept on; } http { ## # Basic Settings ## sendfile on;# 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载.注意:如果图片显示不正常把这个改成off. tcp_nopush on;# 防止网络阻塞 tcp_nodelay on;# 防止网路阻塞 keepalive_timeout 65;#连接超时时间 types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; #文件拓展名与文件类型映射表 default_type application/octet-stream;#默认文件类型,默认为text-plain ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
location [PATTERN] #location块,配置请求的路由,以及各种页面的处理情况。
{
# ...
}
root
location中root指定的只是相对路径,需要和路径结合起来映射地址,比如
location ^~/static/ { ## 这里的root需要和路径结合使用,即是映射的文件位置为 /usr/test/static
root /usr/test/;
index index.html
}
alias
alias指定的是绝对路径,不会和location中的路径结合使用,而是直接使用地址映射到文件,比如
location ^~/static/ { ## 不会路径结合映射地址,那么这里就会直接映射到/usr/test/文件夹下的文件
alias /usr/test/;
index index.html
}
一旦配置请求location映射到了指定的位置,那么下面全部的文件夹和文件都可以映射到,不需要再配置对其的映射,但是如果其中的文件名重新映射了地址,那么,这个路径将不能再使用
路径匹配
**一个location定义可以是一个前缀字符串,也可以是一个正则表达式。正则表达式使用的时候要在前面用“”修饰符(用于不区分大小写匹配),或者“*”修饰符(用于区分大小写)。为了找到请求匹配的location,nginx首先检查location定义,用前缀字符串(这些location成为前缀location)。其中,最长匹配前缀的location会被选中并记住。然后,检查正则表达式,按照它们在配置文件中出现的顺序。对正则表达式的搜索在第一次匹配时终止,并使用相应的配置。如果没有找到与正则表达式的匹配,则使用前面记住的前缀位置的配置 **
优先使用正则表达式,如果没有匹配的正则表达式发现,则使用匹配的最长前缀字符串location
优先级
location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 # 但是正则和最长字符串会优先匹配 [ configuration B ] } location /documents/ { # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索 # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条 [ configuration C ] } location ~ /documents/Abc { # 匹配任何以 /documents/Abc 开头的地址,匹配符合以后,还要继续往下搜索 # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条 [ configuration CC ] } location ^~ /images/ { # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。 [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配所有以 gif,jpg或jpeg 结尾的请求 # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则 [ configuration E ] } location /images/ { # 字符匹配到 /images/,继续往下,会发现 ^~ 存在 [ configuration F ] } location /images/abc { # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在 # F与G的放置顺序是没有关系的 [ configuration G ] } location ~ /images/abc/ { # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用 # 因为都是正则匹配,优先级一样,选择最上面的 [ configuration H ] }
反向代理 proxy_pass
在nginx 中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走
如下四种情况分别访http://192.168.0.1/proxy/test.htmll
#第一种:末尾加斜杠,proxy_pass中不包含路径 location /proxy/ { proxy_pass http://127.0.0.1/; } #代理到URL:http://127.0.0.1/test.html,proxy_pass+请求url匹配的location路径后的内容 #第二种(相对于第一种,最后少一个 / ),末尾不加斜杠,proxy_pass中不包含路径 location /proxy/ { proxy_pass http://127.0.0.1; } #代理到URL:http://127.0.0.1/proxy/test.html,proxy_pass替换请求url的ip和端口 #第三种:末尾加斜杠,proxy_pass中包含路径 location /proxy/ { proxy_pass http://127.0.0.1/aaa/; } #代理到URL:http://127.0.0.1/aaa/test.html,proxy_pass+请求url匹配的location路径后的内容 #第四种(相对于第三种,最后少一个 /,末尾不加斜杠,url中包含路径 )proxy_pass+请求url匹配的location路径后的内容 location /proxy/ { proxy_pass http://127.0.0.1/aaa; } #代理到URL:http://127.0.0.1/aaatest.html
总结
- 如果proxy_pass后面有斜杠,转发url为proxy_pass+原url匹配的location路径之后的内容。 例:原请求http://192.168.10.1/proxy/test.html, location 为/proxy/ proxy_pass为 http://127.0.0.1:81/abc/ 转发路径:(proxy_pass)http://127.0.0.1:81/abc/加上原请求部分路径test.html,最终路径http://127.0.0.1:81/abc/test.html
- 如果proxy_pass后面没有斜杠
- proxy_pass只有ip加端口无路径时。匹配规则为proxy_pass替换原请求url的ip和端口, 同时保留了location路径。例子为上述的第二种情况。
- 当proxy_pass中端口后包含路径时,匹配规则同1(后面有斜杠)
先上配置
server { listen 443 ssl;#新版本通过在端口后添加ssl 开启ssl server_name www.****.com; #域名或ip地址 #ssl on; #旧版本通过ssl on 开启ssl ssl_certificate /xxx.crt;#证书地址 ssl_certificate_key /xxx.key;#生成证书申请文件对应的私钥 ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置 ssl_prefer_server_ciphers on; location / { root html; #站点目录,此时通过/ 访问到的路径是html/index.html 或 html/index.htm index index.html index.htm; #try_files $uri $uri/ /index.html #指向入口文件,vue中常用 } }
通过以上配置就可以通过https 访问了,但是很多用户可能还是会通过http的方式来访问,这个时候就需要通过将http转发至https,只需要在你的http server 下配置 rewrite ^ https://$http_host$request_uri? permanent;
即可 ,如下配置 将http 80端口转发至https 444端口
server { listen 80; server_name www.***.com; rewrite ^ https://$http_host$request_uri? permanent; # force redirect http to https } server { listen 443; server_name www.****.com; #填写绑定证书的域名 ssl on; ssl_certificate /xxx.crt; ssl_certificate_key /xxx.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置 ssl_prefer_server_ciphers on; location / { root html; #站点目录 index index.html index.htm; } location /proxy { proxy_pass http://xx.xx.xx.xx:port/xx.xx; #https 转发至http 具体转发规则可以参考上面反向代理部分 } }
老规矩,先上配置
http { # ... 省略其它配置 upstream tomcats { # 定义一个负载集群 tomcats server 192.168.0.100:8080; server 192.168.0.101:8080; server 192.168.0.102:8080; } server { listen 80; location / { proxy_pass http://tomcats; #将80端口的请求代理到负载均衡集群中 } } # ... 省略其它配置 }
通过上面配置,访问80的请求可以依次的交给100,101 ,102这三台服务器来梳理,可以通过配置不同的参数来改变分配策略
upstream tomcats {
server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15 max_conns=1000;
server 192.168.0.101:8080 down;
server 192.168.0.102:8080 backup;
}
nginx 负载均衡通过upstream 模块来实现的,nginx 内置的三种负载均衡策略
7. 轮询(默认),Nginx根据请求次数,将每个请求均匀分配到每台服务器
8. 最少链接,将请求分配给连接数最少的服务器,Nginx会统计哪些服务器的连接数最少。
9. IP Hash ,绑定处理器请求的服务器,第一次请求时,根据该客户的ip地址算出一个hash值,将请求分配到集群中的某一台服务器上,后面该客户的所有请求,都将通过hash算法,找到之前处理这台客户端请求的服务器,然后请求交给它来处理。
1.1 删除nginx,–purge包括配置文件 sudo apt-get --purge remove nginx 1.2 自动移除全部不使用的软件包 sudo apt-get autoremove 1.3 罗列出与nginx相关的软件 dpkg --get-selections|grep nginx 执行1.3的结果: stephen@stephen-OptiPlex-390:~$ dpkg --get-selections|grep nginx nginx install nginx-common install nginx-core install 1.4 删除1.3查询出与nginx有关的软件 sudo apt-get --purge remove nginx sudo apt-get --purge remove nginx-common sudo apt-get --purge remove nginx-core 这样就可以完全卸载掉nginx包括配置文件 p nginx nginx install nginx-common install nginx-core install 1.4 删除1.3查询出与nginx有关的软件 sudo apt-get --purge remove nginx sudo apt-get --purge remove nginx-common sudo apt-get --purge remove nginx-core 这样就可以完全卸载掉nginx包括配置文件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。