一、alias实现虚拟目录 alias与root的用法区别:
最基本的区别:alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录。另外,根据前文所述,使用alias标签的目录块中不能使用rewrite的break。
When location matches the last part of the directive’s value:
location /p_w_picpaths/ {
alias /data/w3/p_w_picpaths/;
}
it is better to use the root directive instead:
location /p_w_picpaths/ {
root /data/w3;
}
二、反向代理:
location匹配命令
http://nginx.org/en/docs/http/ngx_http_core_module.html#location
A location can either be defined by a prefix string, or by a regular expression.
Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching)
一个location可以用prefix string(前缀字符串)定义,也可以通过regular expression(正则表达式来定义)
通俗的说也就是:我们可以通过使用不同的前缀,表达不同的含义,对于不同的前缀可以分为两大类:普通location和正则location
符号:”~”表示uri包含正则,并且区分大小写
符号:“~*”表示uri包含正则,但不区分大小写
注意:如果你的uri用正则,则你的正则前面必须添加~或者~*,之前我在这里存在误区,以为可以不加~或者~*
To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
Nginx服务器会首先会检查多个location中是否有普通的uri匹配,如果有多个匹配,会先记住匹配度最高的那个。然后再检查正则匹配,这里切记正则匹配是有顺序的,从上到下依次匹配,一旦匹配成功,则结束检查,并就会使用这个location块处理此请求。如果正则匹配全部失败,就会使用刚才记录普通uri匹配度最高的那个location块处理此请求。
If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.
当普通匹配的最长前缀匹配有符号“^~”的时候,就不会在匹配正则
直接使用当前匹配的这个location块处理此请求
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.
使用符号“=”修饰符可以定义一个精确匹配的URI和位置,如果找到了一个精确的匹配,则搜索终止,例如,如果一个”/”请求频繁发生,定义“location =/”将加快这些请求的处理,一旦精确匹配只有就结束,这样的location显然不能包含嵌套location
这里我们说一下location / {} 和location =/ {}的区别:
“location / {}”是普通的最大前缀匹配,任何的uri肯定是以“/”开头,所以location / {} 可以说是默认匹配,当其他都不匹配了,则匹配默认匹配
根据上述官网内容进行总结
a. ”=”用于普通uri前,要求精确匹配,如果匹配成功,则停止搜索并用当前location处理此请求
b. ”~” 表示uri包含正则,并且区分大小写
c. “~*”表示uri包含正则,但不区分大小写
d. ”^~”表示在普通uri前要求Nginx服务器找到普通uri匹配度最高的那个location后,立即处理此请求,并不再进行正则匹配
e. ”^~”和“=”都可以阻止继续匹配正则location两者的区别:“^~”依然遵守最大前缀原则,然后“=”是需要严格匹配
关于location网上的一些误解
location 的匹配顺序是“先匹配正则,再匹配普通”
这是一个错误的结论,从上面官网的文章中我们可以知道:
先匹配普通uri,然后记住匹配度最高的那个(官网原话:To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered.)然后匹配正则,如果正则匹配则结束查找,如果正则不匹配,则匹配之前普通匹配中匹配度最高的那个将执行该请求(官网原话:Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.)
所以:location 的匹配顺序是“先匹配正则,再匹配普通” 这句话肯定是错误的,况且这里并没有包含”^~”和“=”
location 的执行逻辑跟 location 的编辑顺序无关。
这也是一种错误的理解,我们根据上述内容可以知道:
如果是普通uri 匹配,这个时候是没有顺序的,但是正则匹配则是有顺序的,是从上到下依次匹配,一旦有匹配成功,则停止后面的匹配。
举例:第一种例外:
location ~* ^/bbs {
proxy_pass http://192.168.100.100:8080
proxy_pass http://192.168.100.100:8080/form 注:此写法不允许,会报错
}
则bbs是通过模式匹配到的,所以bbs则会被传到上游服务器,最后转到的是http://192.168.100.100:8080/bbs,而无需在proxy_pass指定其他路径。
三、主配置文件配置:
#user nobody;
worker_processes 4;
error_log logs/error.log debug_http;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,
worker_connections 1024; #单个后台worker process进程的最大并发链接数
# 并发总数是 worker_processes 和 worker_connections 的乘积
# 即 max_clients = worker_processes * worker_connections
# 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么
# 为什么上面反向代理要除以4,应该说是一个经验值
# 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000
# worker_connections 值的设置跟物理内存大小有关
# 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
# 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右
# 我们来看看360M内存的VPS可以打开的文件句柄数是多少:
# $ cat /proc/sys/fs/file-max
# 输出 34336
# 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
# 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
# 使得并发总数小于操作系统可以打开的最大文件数目
# 其实质也就是根据主机的物理CPU和内存进行配置
# 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
# ulimit -SHn 65535
}
http {
include mime.types; #设定mime类型,类型由mime.type文件定义
include /usr/local/nginx/conf/conf.d/*.conf; #包含其他文件
default_type application/octet-stream;
sendfile on;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
#对于普通应用,必须设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
#以平衡磁盘与网络I/O处理速度,降低系统的uptime.
#tcp_nopush on;
keepalive_timeout 120; #连接超时时间
tcp_nodelay on;
server {
listen 80;
server_name localhost;
access_log logs/access.log;
error_log logs/error.log;
charset utf-8;
location / {
index index.html index.htm;
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
四、虚拟主机:
conf.d目录下添加vhosts.conf
server {
listen 172.16.171.101:80;
server_name www.wushank.com;
charset utf-8;
error_log logs/wushank/error.log;
access_log logs/wushank/access.log;
location / {
root /var/www/html/wushank;
index index.html index.htm;
}
}
server {
listen 172.16.171.101:80;
server_name www.test.com;
charset utf-8;
error_log logs/test/error.log;
access_log logs/test/access.log;
location / {
root /var/www/html/test;
index index.html index.htm;
}
}
server {
listen 10.1.1.100:8000;
server_name localhost;
charset utf-8;
error_log logs/wang/error.log;
access_log logs/wang/access.log;
location / {
root /wang;
index index.html index.htm;
}
}
四、反向代理配置:
upstream f5-test {
server 172.16.171.100:8080;
server 172.16.171.110:80;
server 172.16.171.120:80;
}
server {
listen 80;
server_name 172.16.171.100 f5.sky.com;
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#设置反向代理的全局信息
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#根据URI对不同的目录进行反向代理
location /f5/ { 注:URL后有“/",则在转到上游URL后也需要有"/"
access_log logs/f5/access.log;
error_log logs/f5/error.log;
proxy_pass http://f5-test/f5/;
proxy_redirect default;
}
location /wushank { 注:URL后无“/",则在转到上游URL后也无"/"
access_log logs/wushank/access.log;
error_log logs/wushank/error.log;
proxy_pass http://f5-test/wushank;
proxy_redirect default;
}
}
转载于:https://blog.51cto.com/wushank/1621543