CenOS 7安装nginx-源码安装方式
文章目录
1.安装依赖库gcc、pcre pcre-devel、zlib、openSSL
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.下载Stable版nginx包
3.安装nginx
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure
make && make install
4.启停nginx
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
5.开机启动
vi /etc/rc.local
追加
/usr/local/nginx/sbin/nginx
6.防火墙
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
7.反向代理配置
location /arcgis/ {
proxy_pass http://192.168.1.100:6080/arcgis/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
index index.html index.htm;
}
8.图片缓存配置
- 缓存策略配置
proxy_cache_path配置放于http模块内
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=imgcache:300m inactive=1d max_size=5g;
- 缓存配置
add_header Nginx-Cache "$upstream_cache_status";
增加响应头;
proxy_cache imgcache;
缓存域,请于proxy_cache_path中keys_zone保持一致。
expires 1d;
对应响应头Cache-Control
,缓存时间1天;
·proxy_cache_valid 200 302 1d;代办nginx服务端缓存1天(响应码为200 302情况下)。
location /portal/getNewsFile {
proxy_pass http://192.168.1.100:8080/portal/getNewsFile;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
index index.html index.htm;
expires 1d;
proxy_cache imgcache;
proxy_cache_key $host$uri$is_args$args;
add_header Nginx-Cache "$upstream_cache_status";
proxy_cache_valid 200 302 1d;
proxy_cache_valid 404 10s;
proxy_cache_valid any 1s;
}
说明: 启用proxy_cache_valid 200 302 1d;
nginx服务端缓存,可不再启用客户端缓存expires 1d;
。
- ngx_cache_purge
通过url清理指定缓存(编译ngx_cache_purge模块已获得支持);
清理http://192.168.1.100/test.txt
,只需要执行http://192.168.1.100/purge/test.txt
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
proxy_cache_purge imgcache $host$1$is_args$args;
access_log logs/cache.log cache;
}
- 手动缓存清理脚本
cat >purge-cache.sh<<EOF
rm -rf /var/cache/nginx/*
EOF
chmod +x purge-cache.sh
参考:
nginx-缓存proxy_ cache和ngx_cache_purge
9.更多配置
# 隐藏nginx版本号
server_tokens off;
# 合并多余/以避免多次重定向
merge_slashes on;
参照:
官方配置
10.问题汇总
问题:nginx外网端口映射,外部端口(10080)和nginx 内部端口(80)不一致情况下,访问
http://xxx.xxx.xxx.xxx:10080/arcgis
若不以/
结尾,会自动重定向至http://xxx.xxx.xxx.xxx:80/arcgis/
,导致无法正常访问服务!
解决办法:将nginx 内部端口更改为10080,使nginx外网映射端口和内网端口保持一致,清理浏览器缓存后重试!(网上建议是将在 http, server, location 上下文内增加port_in_redirect off;
,尝试无效!)