Skip to content

Instantly share code, notes, and snippets.

@SherryQueen
Created November 1, 2017 06:55
Show Gist options
  • Select an option

  • Save SherryQueen/142e5a34c20f91f0188988debce2a1ee to your computer and use it in GitHub Desktop.

Select an option

Save SherryQueen/142e5a34c20f91f0188988debce2a1ee to your computer and use it in GitHub Desktop.
nginx location 学习
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
# 已=开头表示精确匹配
# 如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。
# ^~ 开头表示uri以某个常规字符串开头,不是正则匹配
# ~ 开头表示区分大小写的正则匹配;
# ~* 开头表示不区分大小写的正则匹配
# / 通用匹配, 如果没有其它匹配,任何请求都会匹配到
# (location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)
listen 3000;
server_name localhost;
location = / {
# 精确匹配 优先度最高
# 后面不能带字符串
proxy_pass https://www.bing.com;
}
location / {
# 匹配所有请求 但最长字符串和正则优先度大于该匹配
# e.g. /s
# e.g. ^~ /images/
proxy_pass https://www.bilibili.com;
}
location /s {
proxy_pass https://www.baidu.com/s;
}
location /images {
# 匹配images 开头的连接
# 匹配符合以后,还要继续往下搜索
proxy_pass https://www.baidu.com/s;
}
location ^~ /images/ {
# 匹配以 images 开头的连接
# 匹配符合后不往下继续搜索
# e.g. /images/** 可匹配
# e.g. /**/images/ 不匹配
proxy_pass https://cn.bing.com/images;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment