我看到这里也有人问这个问题:Configure Nginx global error page但没有合适的答案。
我目前只允许某些 IP 地址访问我的某些站点,并希望 Nginx 删除其余 IP 地址的连接,而不是返回 403。理想情况下,我希望在不单独配置每个站点的情况下执行此操作。
我在这个http部分有这个:
error_page 403 =444;
Run Code Online (Sandbox Code Playgroud)
但是,现在,它尝试提供页面 =444,而不是返回 444。
在我的Nginx配置中我有
location / {
allow 198.168.0.1;
deny all;
}
Run Code Online (Sandbox Code Playgroud)
现在,我想将除允许的 IP 之外的所有其他 IP 重定向到google.com,我该怎么做?
我试过类似的东西
location / {
allow 198.168.0.1;
deny all;
rewrite ^ http://google.com/;
}
Run Code Online (Sandbox Code Playgroud)
但这会直接重定向到google.com所有IP。我只想198.168.0.1访问该页面,其他页面将被重定向。
我刚刚开始学习 nginx 和 SSL,并且一直在尝试不同的配置。我有两个服务器块,如下所示:
server{
listen 443 ssl http2;
server_name www.a.com;
ssl_certificate a.crt;
ssl_certificate_key a.key;
....
}
server{
listen 443 ssl ;
server_name www.b.com;
ssl_certificate b.crt;
ssl_certificate_key b.key;
....
}
Run Code Online (Sandbox Code Playgroud)
在使用 www.a.com 作为我的 SNI 通过 openssl s_client 连接到 nginx 后,我使用 Host: www.b.com 发送了一个 GET 请求,它仍然有效。这是预期的吗?有人可以帮助我了解 nginx 的行为吗?
我正在尝试在我的 CentOS 7 VPS 上设置 Nginx,我已经安装了 Nginx、MariaDb 和 PHP。
但是当我跑步时,nginx -t我得到:
nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1014000 in /usr/share/nginx/modules/mod-http-geoip.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我什至没有接触任何ngx_http_geoip_module.so相关的东西。
版本信息
nginx版本:
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module …Run Code Online (Sandbox Code Playgroud) 我每 30 分钟下载一次 Tor 出口节点列表https://www.dan.me.uk/torlist/并将其保存到 tor-ip.conf 文件。
我的主机不允许为 nginx 安装任何模块,所以我必须使用我拥有的模块。
我想将文件包含到一个变量中,并在匹配时调用重定向。
配置文件
$bad_user {
include /var/www/vhosts/domain/tor-ip.conf;
}
if ($bad_user) {
rewrite ^ http://www.google.com;
}
Run Code Online (Sandbox Code Playgroud)
tor-ip.conf
1.41.177.72
1.42.47.215
100.0.53.178
100.15.114.155
100.16.220.246
100.34.251.120
100.36.123.111
100.6.14.127
100.7.25.216
101.100.144.174
101.175.68.120
101.176.45.79
...
Run Code Online (Sandbox Code Playgroud)
Nginx 不允许我直接使用变量作为$bad_user. 我怎么能设置呢?
对 nginx 完全陌生,我需要一个 conf 文件来使 nginx 充当反向代理,以在同一主机上向具有不同 url 路径的 elasticsearch 和 kibana 提供请求。我的意思是,我想要:
localhost/es -> localhost:9200
localhost/kibana -> localhost:5601
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到?
谢谢
我发现在做Nginx的日志旋转的例子在这里
但是一个简单的测试:
set $date "2018-08-24";
access_log /home/tim/log/access-http-$date.log default;
Run Code Online (Sandbox Code Playgroud)
生成一个名为access-http-.log. 我正在使用 nginx 1.13.6(带有 openresty)。
更新
经过大量的修改和调整后,我想出了以下 logrotate 脚本来旋转 nginx 生成的不同日志文件。我已经把它放到了 /etc/logrotate.conf 文件中。剩下的问题是日志不会每天轮换(我目前不确定为什么),但是 alogrotate -f <filename>产生的结果正是我想要的。
一个有趣的注意事项是nginx -s reload可以代替 USR1 使用。不幸的是,nginx 日志记录页面中没有引用它,但我找到了它(在手册页 IIRC 中)。我手动构建 openresty/nginx 以合并我需要的额外模块,因此我不会像 pid 保留那样获得打包版本中的各种额外功能。
/path/to/log/access-http.log /path/to/log/access-https.log /path/to/log/api.log /path/to/log/error.log {
daily
missingok
notifempty
create 664 nobody tim
dateext
dateformat -%Y-%m-%d
dateyesterday
rotate 10
su tim tim
postrotate
/usr/local/bin/openresty -s reload
endscript
}
Run Code Online (Sandbox Code Playgroud)
我认为这对于拥有大型 nginx 配置同时提供网页和 API 的任何人都非常有用。我将 http 分开,因为我不提供非 https 页面,并且它将脚本小子废话从我的页面日志中删除。
我有时在端口 81 中使用 apache 并使用 nginx 在端口 80(或 443)中运行,就像这样
location / {
proxy_pass http://localhost:81
}
Run Code Online (Sandbox Code Playgroud)
我必须这样做,因为某些软件(如 phpbb 在 nginx 上无法正常运行)这是个好主意吗?
运行“telnet website.com 80 HEAD / HTTP/1.0”声称服务器是nginx/1.14.1,但是在站点上启用了目录浏览并且访问它时会清楚地显示“Apache Server at website.com Port 80”。什么可能导致这种情况?
我提供了一个 wordpress 博客,其中 nginx http 缓存到超过 99% 的请求,缓存寿命为 2 天。这是该网站的网页。网页有很多图像,因此延迟加载。页面的平均大小仅为 1 mb。
平均响应大小为 10 KB
使用速度曲线时,我的 TTFB 中位数为 0.6 秒
为什么这么高?
我支持 cloudflare,所有静态资产(如 JS、CSS 和图像)都从 cloudflare 进行版本控制和缓存。
我的 nginx 配置有
sendfile on;
# https://forum.nginx.org/read.php?2,280434,280434#msg-280434
tcp_nopush on;
tcp_nodelay on;
#https://support.cloudflare.com/hc/en-us/articles/212794707-General-Best-Practices-for-Load-Balancing-at-your-origin-with-Cloudflare
#https://www.nginx.com/blog/tuning-nginx/
keepalive_timeout 300s;
keepalive_requests 10000;
Run Code Online (Sandbox Code Playgroud)
我也有
initcwnd 设置为 10,initrwnd 10 和 ipv4.tcp_slow_start_after_idle=0
这是 cloudflare 对从 CF 到源的响应时间的报告

静态网站也在两台机器上进行负载平衡。一个在弗里蒙特,一个在孟买,Cloudflare 做地理路由。
为什么我的 TTFB 这么长,我可以做些什么来减少它?
nginx ×10
apache-2.4 ×2
redirect ×2
centos7 ×1
http-caching ×1
kibana ×1
logging ×1
openresty ×1
openssl ×1
performance ×1
sni ×1
ssl ×1
tor ×1