Nginx反向代理/location/URL重写功能实战
内容介绍:
一、Nginx反向代理常用实战
二、location 应用实例
一、Nginx反向代理常用实战
1.多域名跳跃:
Server www.tb.com
location/{
proxy_pass http://192.168.66.90:8080/web/
}
location/admin{
proxy_passhttp://192.168.66.90:8080/admin
}
serverm.tb.com
location /{
proxy_pass http://192.168.66.90:8080/wap/
}
打开vi nginx.conf,重点看实现的功能
[root@localhost conf]# vi nginx.conf,
打开浏览器访问www.tb.com
需要启动才能进入网页,connect() failed连接异常,修改最后的符合,改为/(反向代理)
把某个请求放到某个目录的时候在最后的代理一定要加上“/”,不加“/”则会报错。
前面跟的符号是什么,后面的符号就同前面的符号一样。
nginx. conf" 131L, 2647C written
[ root@loca lhost conf ]# killall -9 nginx
[ root@loca lhost conf ] #. ./sbin/ nginx
nginx: [warn] 65536 worker_ connections exceed open file resource 1 imit: 1024
2.新旧域名过渡:
server{
server_ name www.taob.com;
rewrite^/(.*)$ http://www.tb.com/$1 permanent;
{
301是临时重定项,302是永久重定项
例如:
server{
Isten 80;
server_name www. taobao.com;
#charset koi8-r;
access_log logs/taobao.access.log main;
rewrite ^/(.*)$ http://www.taob.com/$1 permanent;
#location ~ ^/best/ {
#rewrite ^/best/(.*)$ /test/$1 break;
#proxy_passhttp://www.taob.com;
# }
实现方法,当用户访问www. taobao.com,都将他调用
3.alias与root功能
#gzip on;
server {
listen 80;
server_name www.iivey.com;
#charset koi8-r;
access_log logs/host.access.log main;
location /a {
alias /usr/local/nginx/html/images/;
}
location /f {
root /usr/local/nginx/html/images/;
}
location = / {
proxy_pass http://192.168.81.236:8080/;
}
[ root@localhost logs]# cd . .
[ root@localhost nginx]# ls
client body_ temp conf fastcgi temp html logs proxy_ temp sbin
[ root@localhost nginx]# cd html/
[ root@localhost htm1 ]# ls
50x. html images index.html info.php test
[ root@localhost htm1 ]# cd images/
[ root@localhost images ]# ls
56a64966N7ed76ac3. jpg f iivey . png wK iom1NUhYXT iK7AAAMWMryyZ_ c708. Jpg
[ root@localhost images ]# 1 1
total 268
[ root@localhost images ]# pwd
[ rootalocalhost images ] #
在f目录里访问index.html。
alias:指定请求,可以在alias直接在目录下找到请求
Root:必须有f这个目录才能找对应的目录
对比:
location/i {
root /var/www/html/images/;
二、location应用实例
location匹配规则优先级:
location=/{
[configA]
{