Nginx下简单的域名重定向和代理

相同域名的域名重定向
http://www.localhost.com --> https://www.localhost.com
方法一:

server {
        listen       80;
        server_name  www.localhost.com localhost.com;
       rewrite  "^/(.*)$"  https://${host}/$1 permanent;
}

方法二:

server {
        listen       80;
        server_name  www.localhost.com localhost.com;
        return 301 https://$host$request_uri;
}

PS:

$server_name 只能匹配第一个域名
$host 可以匹配到对应域名

 

 

ThinkPHP伪静态

        if (!-e $request_filename) {
           rewrite  ^/(.*)$  /index.php/$1  last;
           break;
        }

 域名跳转

1.域名不变
server {
listen       80;
server_name  old.yagm.com.cn;
        location /
         {
        proxy_pass http://www.yagm.com.cn/;
          }
        }

	
2.域名也跳转
server {
listen     80;
server_name  old.yagm.com.cn;
rewrite  "^/(.*)$"  http://www.yagm.com.cn/$1 break;
          }

伪静态

举例:
    rewrite ^/blog/u/\d+/blog/(\d+).html$ /blog/detail/id/$1.html break;
    rewrite ^/article/(\d+).html$ /news/$1.html break;
    rewrite ^/blog/u/(\d+)/blog.html$ /blog/index/uid/$1.html break;
    rewrite ^/blog/u/(\d+)/video/(\d+).html$ /video/detail/id/$2.html break;
    rewrite ^/blog/u/(\d+)/photo/album/(\d+).html$ /album/photo/id/$2.html break;

proxy_pass代理 

IP跳转
server {
        listen       80;
        server_name  domain;

         location / {
                proxy_pass http://ip;
                proxy_redirect off;
                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 10m;
                client_body_buffer_size 128k;
                proxy_connect_timeout 90;
                proxy_send_timeout 90;
                proxy_read_timeout 90;
                proxy_buffer_size 128k;
                proxy_buffers 32 32k;
                proxy_busy_buffers_size 128k;
                proxy_temp_file_write_size 256k;
        }

    }


域名跳转
server{
        listen 80;
        server_name domain;


         location / {
                proxy_pass http://yuming;
                proxy_redirect http://yuming/ /;
                #proxy_set_header HOST $host;
                proxy_set_header et_title $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size 600m;
                client_body_buffer_size 128k;
                proxy_connect_timeout 600;
                proxy_send_timeout 600;
                proxy_read_timeout 600;
                proxy_buffer_size 128k;
                proxy_buffers 32 32k;
                proxy_busy_buffers_size 128k;
                proxy_temp_file_write_size 256k;
        }
}

break、last、redirect、permanent区别 

配置

# cat rewrite.conf 
server {
    listen 8096;
    server_name _;
    root /opt/app/code;
    location ~ ^/break {
        rewrite ^/break /test/ break;    # break 后,后面的指令也不会执行了。如果没有 break ,就会执行后面的指令,比如这里的 return。
        return 200 'break';
    }   
    location ~ ^/last {
        rewrite ^/last /test/ last;    # last 后,后面的指令也不会执行了。如果没有 last ,就会执行后面的指令,比如这里的 return。
        return 200 'last';
    }
    location ~ ^/redirect {
        rewrite ^/redirect /test/ redirect;    # redirect 后,后面的指令也不会执行了。如果没有 redirect,就会执行后面的指令,比如这里的 return。
        return 200 'redirect';
    }
    location ~ ^/permanent {
        rewrite ^/permanent /test/ permanent;    # permanent 后,后面的指令也不会执行了。如果没有 permanent ,就会执行后面的指令,比如这里的 return。
        return 200 'permanent';
    }
    location /test/ {
        default_type application/json;
        return 200 '{"status": "sucess"}';
    }
}
# ll /opt/app/code/
total 0

结果

  1. 请求 /test/ 返回预期的结果
# curl 192.168.1.188:8096/test/
{"status": "sucess"}
  1. 请求 /break 返回 404,因为匹配到 ^/break 后,^/break rewrite 为 /test/ ,就 break 了,访问 /test/ 但 /opt/app/code/ 里并没有这个目录
# curl 192.168.1.188:8096/break
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>
  1. 请求 /last 返回了 location /test/ 的结果,因为 last 会让 rewrite 后的 /test/ 请求在 server 块重新匹配一遍,就配置到了 location /test/
# curl 192.168.1.188:8096/last
{"status": "sucess"}
  1. redirect 结果(客户端每次请求都会执行一次请求跳转)
# curl -vL 192.168.1.188:8096/redirect
* About to connect() to 192.168.1.188 port 8096 (#0)
*   Trying 192.168.1.188... connected
* Connected to 192.168.1.188 (192.168.1.188) port 8096 (#0)
> GET /redirect HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.1.188:8096
> Accept: */*
> 
< HTTP/1.1 302 Moved Temporarily
< Server: nginx/1.10.2
< Date: Wed, 28 Feb 2018 18:46:48 GMT
< Content-Type: text/html
< Content-Length: 161
< Location: http://192.168.1.188:8096/test/
< Connection: keep-alive
< 
* Ignoring the response-body
* Connection #0 to host 192.168.1.188 left intact
* Issue another request to this URL: 'http://192.168.1.188:8096/test/'
* Re-using existing connection! (#0) with host 192.168.1.188
* Connected to 192.168.1.188 (192.168.1.188) port 8096 (#0)
> GET /test/ HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.1.188:8096
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.10.2
< Date: Wed, 28 Feb 2018 18:46:48 GMT
< Content-Type: application/json
< Content-Length: 20
< Connection: keep-alive
< 
* Connection #0 to host 192.168.1.188 left intact
* Closing connection #0
{"status": "sucess"}

  1. permanent结果(不清理缓存的话,客户端每次请求都会用 permanent 后的地址)
# curl -vL 192.168.1.188:8096/permanent
* About to connect() to 192.168.1.188 port 8096 (#0)
*   Trying 192.168.1.188... connected
* Connected to 192.168.1.188 (192.168.1.188) port 8096 (#0)
> GET /permanent HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.1.188:8096
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.10.2
< Date: Wed, 28 Feb 2018 18:48:44 GMT
< Content-Type: text/html
< Content-Length: 185
< Location: http://192.168.1.188:8096/test/
< Connection: keep-alive
< 
* Ignoring the response-body
* Connection #0 to host 192.168.1.188 left intact
* Issue another request to this URL: 'http://192.168.1.188:8096/test/'
* Re-using existing connection! (#0) with host 192.168.1.188
* Connected to 192.168.1.188 (192.168.1.188) port 8096 (#0)
> GET /test/ HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.1.188:8096
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.10.2
< Date: Wed, 28 Feb 2018 18:48:44 GMT
< Content-Type: application/json
< Content-Length: 20
< Connection: keep-alive
< 
* Connection #0 to host 192.168.1.188 left intact
* Closing connection #0
{"status": "sucess"}

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值