相同域名的域名重定向
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
结果
# curl 192.168.1.188:8096/test/
{"status": "sucess"}
# curl 192.168.1.188:8096/break
404 Not Found
404 Not Found
nginx/1.10.2
# curl 192.168.1.188:8096/last
{"status": "sucess"}
# 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"}
# 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"}