1. nginx中location规则:
规则 | 描述 |
---|---|
~ | 表示执行一个正则匹配,区分大小写 |
~* | 表示执行一个正则匹配,不区分大小写 |
^~ | 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录 |
= | 进行普通字符精确匹配 |
@ | 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files |
~和~*同时存在的时候,从上往下进行匹配
~和^~同时存在的时候,优先是^~
=匹配的优先级别最高
@使用场景:
laravel的伪静态和swoole假设laravel框架,进行反向代理的时候会用到这个,还有异常处理转向操作
2. nginx的rewrite指令
www.baidu.com/abc => www.aliyun.com/bcd
Rewrite是nginx服务器提供的一个重要基本功能,在Web服务器产品中几乎是必备的
功能,主要用于实现URL的重写。
URL的重写能让我们在改变网站结构后,无需要求客户端用户修改原来的书签,也无
需其他网站修改对我们网站的链接地址,能够提高网站的安全性
rewrite指令通过正则表达式的使用来改变URI。可以同时存在一个或者多个指令,
按照顺序依次对URL进行配置和处理
参数:
regex :用于匹配URI的正则表达式。
replacement:匹配成功后用于替换URI中截取内容的字符串
flag:用来设置rewrite对URI的处理行为
rewrite ^/abc /index.php redirect; #临时重定向 301 rewrite ^/bcd /index.php permanent; #永久重定向 302
查阅nginx中重定向break的使用
user www www;
worker_processes auto;
error_log logs/nginx_error.log crit;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;events{use epoll;worker_connections 51200;multi_accept on;}http{include mime.types;lua_package_path "/www/server/luaJIT/lib/lua/?.lua";include proxy.conf;default_type application/octet-stream;server_names_hash_bucket_size 512;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_max_body_size 50m;sendfile on;tcp_nopush on;keepalive_timeout 60;tcp_nodelay on;fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 256k;fastcgi_intercept_errors on;gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.1;gzip_comp_level 2;gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;gzip_vary on;gzip_proxied expired no-cache no-store private auth;gzip_disable "MSIE [1-6]\.";limit_conn_zone $binary_remote_addr zone=perip:10m;limit_conn_zone $server_name zone=perserver:10m;server_tokens off;access_log off;server{listen 888;server_name phpmyadmin;index index.html index.htm index.php;root /www/server/phpmyadmin;location ~ /tmp/ {return 403;}#include enable-php.conf;location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;}location ~ .*\.(js|css)?${expires 12h;}location ~ /\.{deny all;}access_log logs/access.log;}include /www/server/nginx/vhost/*.conf;
}# configuration file /www/server/nginx/conf/mime.types:types {text/html html htm shtml;text/css css;text/xml xml;image/gif gif;image/jpeg jpeg jpg;application/javascript js;application/atom+xml atom;application/rss+xml rss;text/mathml mml;text/plain txt;text/vnd.sun.j2me.app-descriptor jad;text/vnd.wap.wml wml;text/x-component htc;image/png png;image/svg+xml svg svgz;image/tiff tif tiff;image/vnd.wap.wbmp wbmp;image/webp webp;image/x-icon ico;image/x-jng jng;image/x-ms-bmp bmp;font/woff woff;font/woff2 woff2;application/java-archive jar war ear;application/json json;application/mac-binhex40 hqx;application/msword doc;application/pdf pdf;application/postscript ps eps ai;application/rtf rtf;application/vnd.apple.mpegurl m3u8;application/vnd.google-earth.kml+xml kml;application/vnd.google-earth.kmz kmz;application/vnd.ms-excel xls;application/vnd.ms-fontobject eot;application/vnd.ms-powerpoint ppt;application/vnd.oasis.opendocument.graphics odg;application/vnd.oasis.opendocument.presentation odp;application/vnd.oasis.opendocument.spreadsheet ods;application/vnd.oasis.opendocument.text odt;application/vnd.openxmlformats-officedocument.presentationml.presentationpptx;application/vnd.openxmlformats-officedocument.spreadsheetml.sheetxlsx;application/vnd.openxmlformats-officedocument.wordprocessingml.documentdocx;application/vnd.wap.wmlc wmlc;application/x-7z-compressed 7z;application/x-cocoa cco;application/x-java-archive-diff jardiff;application/x-java-jnlp-file jnlp;application/x-makeself run;application/x-perl pl pm;application/x-pilot prc pdb;application/x-rar-compressed rar;application/x-redhat-package-manager rpm;application/x-sea sea;application/x-shockwave-flash swf;application/x-stuffit sit;application/x-tcl tcl tk;application/x-x509-ca-cert der pem crt;application/x-xpinstall xpi;application/xhtml+xml xhtml;application/xspf+xml xspf;application/zip zip;application/octet-stream bin exe dll;application/octet-stream deb;application/octet-stream dmg;application/octet-stream iso img;application/octet-stream msi msp msm;audio/midi mid midi kar;audio/mpeg mp3;audio/ogg ogg;audio/x-m4a m4a;audio/x-realaudio ra;video/3gpp 3gpp 3gp;video/mp2t ts;video/mp4 mp4;video/mpeg mpeg mpg;video/quicktime mov;video/webm webm;video/x-flv flv;video/x-m4v m4v;video/x-mng mng;video/x-ms-asf asx asf;video/x-ms-wmv wmv;video/x-msvideo avi;
}# configuration file /www/server/nginx/conf/proxy.conf:
proxy_temp_path /www/server/nginx/proxy_temp_dir;
proxy_cache_path /www/server/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:20m inactive=1d max_size=5g;
client_body_buffer_size 512k;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_cache cache_one;# configuration file /www/server/nginx/vhost/localhost.conf:
server
{listen 80;server_name 127.0.0.1;root /www/wwwroot/nginx1;index index.php index.html;location ^~ /abc {proxy_cache cache_one;proxy_pass http://yuxiaodong.club/;proxy_cache_valid 200 304 12h;proxy_cache_valid any 10m;proxy_cache_key $host$uri$is_args$args;}location ~ [^/]\.php(/|$){try_files $uri =404;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;include fastcgi.conf;include pathinfo.conf;}
}# configuration file /www/server/nginx/conf/fastcgi.conf:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;# configuration file /www/server/nginx/conf/pathinfo.conf:
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {set $real_script_name $1;set $path_info $2;}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;# configuration file /www/server/nginx/vhost/yuxiaodong.conf:
server
{listen 80;server_name yuxiaodong.club;root /www/wwwroot/blog-web;index index.php index.html;default_type text/html;location ^~ /abc {rewrite ^/abc /index.php redirect; # 临时重定向}location ^~ /bcd {rewrite ^/bcd /index.php permanent; #永久重定向}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 10s;}location ~ [^/]\.php(/|$){try_files $uri =404;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;include fastcgi.conf;include pathinfo.conf;}error_log /www/server/nginx/logs/yuxiaodong_error.log;
}此配置属于nginx -T输出,仅作为参考,不能直接复制,