之前已经在centos7上安装好了nginx,目录在 /etc/nginx/。
其中配置文件是 /etc/nginx/conf.d/default.conf ,
作为反向代理要添加一个路由到本机别的端口可以这样:
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; }
location ^~ /guacamole/ { proxy_pass http://127.0.0.1:4822; } #上面一段表示以 url 以 guacamole 开头的都路由到本机4822端口去
#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 /usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 }
至于什么时候用波浪线?
- ~,波浪线表示执行一个正则匹配,区分大小写
- ~*,表示执行一个正则匹配,不区分大小写
- ^~,表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
- =,进行普通字符精确匹配
- @,定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
location 匹配的优先级(与location在配置文件中的顺序无关)?
= 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
最后匹配理带有""和"*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
尝试理解以下:
location = / { # 只匹配"/". [ configuration A ] } location / { # 匹配任何请求,因为所有请求都是以"/"开始 # 但是更长字符匹配或者正则表达式匹配会优先匹配 [ configuration B ] } location ^~ /images/ { # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location [ configuration C ] } location ~* .(gif|jpg|jpeg)$ { # 匹配以 gif, jpg, or jpeg结尾的请求. # 但是所有 /images/ 目录的请求将由 [Configuration C]处理. [ configuration D ] }
仅以备忘,来自: Nginx location匹配规则 - 简书 (jianshu.com)