Nginx 配置示例技术专题与文档汇总,系统梳理 nginx、demo 核心概念、架构原理、实战配置与开发指南。
示例一:动态代理
server {
listen 80;
server_name xiexianbin.cn;
# logging
access_log /var/log/tengine/xiexianbin.cn.access.log main;
error_log /var/log/tengine/xiexianbin.cn.error.log warn;
location / {
return 301 http://$arg_host-$arg_port.xiexianbin.cn;
}
}
server {
listen 80;
server_name *.xiexianbin.cn;
# logging
access_log /var/log/tengine/xiexianbin.cn.access.log;
error_log /var/log/tengine/xiexianbin.cn.error.log warn;
location / {
if ($http_host ~* ^(.*)-(.*).xiexianbin.cn(.*)) {
set $jupyter_host $1;
set $jupyter_port $2;
}
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_pass http://$jupyter_host:$jupyter_port;
}
}
示例二:代理 SSL
location / {
proxy_pass https://127.0.0.1:443;
proxy_redirect off;
proxy_ssl_verify off;
proxy_ssl_server_name on;
proxy_ssl_session_reuse off;
proxy_set_header Host $host;
...
}允许 iframe 嵌套
- location 下配置,否则报错:
Refused to display 'https://xxx.xiexianbin.cn.cn/' in a frame because it set 'X-Frame-Options' to 'deny'.
# 移除上游返回的 X-Frame-Options 响应头以允许被任意页面通过 iframe 嵌套
proxy_hide_header X-Frame-Options;
# 若上游还返回了限制嵌套的 Content-Security-Policy,也需一并移除或配置允许的域名
# proxy_hide_header Content-Security-Policy;
# add_header Content-Security-Policy "frame-ancestors *" always;
# 说明:X-Frame-Options 规范(RFC 7034)仅包含 DENY 和 SAMEORIGIN 指令(ALLOW-FROM 已被现代浏览器废弃,规范中不存在 ALLOWALL)。
# 若要允许跨域 iframe 嵌套,直接通过 proxy_hide_header 移除该响应头即可,切勿配置非法的 ALLOWALL。Nginx 目录按时间排序
sudo apt-get update
sudo apt-get install libnginx-mod-http-fancyindex
# 确认 load_module modules/ngx_http_fancyindex_module.so;,默认加载的
server {
listen 80;
server_name example.com;
location /downloads/ {
# 关闭原生模块(如果有的话)
# autoindex on;
# 开启 fancyindex
fancyindex on;
# 【核心】设置默认排序为按时间倒序 (最新文件在前)
fancyindex_default_sort date_desc;
# 显示系统本地时间,否则默认显示 UTC 时间
fancyindex_localtime on;
# 关闭精确大小显示,将文件大小转为可读格式(如 MB, GB)
fancyindex_exact_size off;
}
}最近更新
最新评论