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"}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章