首页 > 自动化运维 > > 太强了,终于彻底搞懂 Nginx 的五大应用场景~

user mengday staff;
http { server { listen 80; server_name localhost; client_max_body_size 1024M;
# 默认location location / { root /usr/local/var/www/html; index index.html index.htm; } }}注意:如果访问图片出现403 Forbidden错误,可能是因为nginx.conf 的第一行user配置不对,默认是#user nobody;是注释的,linux下改成user root; macos下改成user 用户名 所在组; 然后重新加载配置文件或者重启,再试一下就可以了, 用户名可以通过who am i 命令来查看。

http { server { listen 80; server_name localhost;
set $doc_root /usr/local/var/www;
# 默认location location / { root /usr/local/var/www/html; index index.html index.htm; }
location ^~ /images/ { root $doc_root; }
location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ { root $doc_root/img; } }}location = / { # 精确匹配/,主机名后面不能带任何字符串 / [ configuration A ]}location / { # 匹配所有以 / 开头的请求。 # 但是如果有更长的同类型的表达式,则选择更长的表达式。 # 如果有正则表达式可以匹配,则优先匹配正则表达式。 [ configuration B ]}location /documents/ { # 匹配所有以 /documents/ 开头的请求,匹配符合以后,还要继续往下搜索。 # 但是如果有更长的同类型的表达式,则选择更长的表达式。 # 如果有正则表达式可以匹配,则优先匹配正则表达式。 [ configuration C ]}location ^~ /images/ { # 匹配所有以 /images/ 开头的表达式,如果匹配成功,则停止匹配查找,停止搜索。 # 所以,即便有符合的正则表达式location,也不会被使用 [ configuration D ]}
location ~* \.(gif|jpg|jpeg)$ { # 匹配所有以 gif jpg jpeg结尾的请求。 # 但是 以 /images/开头的请求,将使用 Configuration D,D具有更高的优先级 [ configuration E ]}
location /images/ { # 字符匹配到 /images/,还会继续往下搜索 [ configuration F ]}
location = /test.htm { root /usr/local/var/www/htm; index index.htm;}server { listen 80; server_name localhost;
location / { proxy_pass http://localhost:8081; proxy_set_header Host $host:$server_port; # 设置用户ip地址 proxy_set_header X-Forwarded-For $remote_addr; # 当请求服务器出错去寻找其他服务器 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; }
}upstream web_servers { server localhost:8081; server localhost:8082; }
server { listen 80; server_name localhost; #access_log logs/host.access.log main;
location / { proxy_pass http://web_servers; # 必须指定Header Host proxy_set_header Host $host:$server_port; } }upstream test { server localhost:8081 weight=1; server localhost:8082 weight=3; server localhost:8083 weight=4 backup;}upstream test { ip_hash; server localhost:8080; server localhost:8081;}upstream backend { fair; server localhost:8080; server localhost:8081;}upstream backend { hash $request_uri; hash_method crc32; server localhost:8080; server localhost:8081;}upstream web_servers { server localhost:8081; server localhost:8082; }
server { listen 80; server_name localhost;
set $doc_root /usr/local/var/www;
location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ { root $doc_root/img; }
location / { proxy_pass http://web_servers; # 必须指定Header Host proxy_set_header Host $host:$server_port; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root $doc_root; }
}location /permanently/moved/url { return 301 http://}location /users/ { rewrite ^/users/(.*)$ /show?user=$1 break;}error_page 404 /404.html;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 /usr/local/etc/nginx/logs/host.access.log main;
gzip on;# 禁止访问某个目录location ~* \.(txt|doc)${ root $doc_root; deny all;}来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31509949/viewspace-3118698/ ,如需转载,请注明出处,否则将追究法律责任。