Nginx生产环境安装配置

不建议使用nginx-1.18.0.tar.gz,因为扫出很多漏洞

上传nginx-1.24.0.tar.gz

[root@zonghe01 data]# ll
-rw-r--r--  1 root root 1112471 Oct 26 15:57 nginx-1.24.0.tar.gz
[root@zonghe01 data]# pwd
/data

解押

[root@zonghe01 data]# tar -zxvf nginx-1.24.0.tar.gz

配置、编译、安装

[root@zonghe01 data]# cd nginx-1.24.0
[root@zonghe01 nginx-1.24.0]# pwd
/data/nginx-1.24.0
[root@zonghe29 nginx-1.24.0]# ./configure --prefix=/data/nginx24
[root@zonghe29 nginx-1.24.0]# make
[root@zonghe29 nginx-1.24.0]# make install

上传配置文件到指定文件夹下(base.conf、xxx.conf、nginx.conf)

/data/nginx24/conf下建立文件夹config和vhost
[root@zonghe01 config]# ll
total 4
-rw-r--r-- 1 root root 1317 Jun 19 09:01 base.conf
[root@zonghe01 config]# pwd
/data/nginx24/conf/config
[root@zonghe01 config]# cd ../vhost/
[root@zonghe01 vhost]# ll
total 12
-rw-r--r-- 1 root root 2723 Nov 17 10:43 xxx.conf
-rw-r--r-- 1 root root  257 Nov 14 11:08 minio.conf

base.conf文件内容

#配置nginx高效的文件传输模式
sendfile        on;
keepalive_timeout  75s;
#隐藏nginx header版本号,默认为on开启
server_tokens off;
#支持请求头参数的下划线,默认为off关闭,请求中的下划线将会被去掉再不往下传递
underscores_in_headers on;
#设置最大上传文件,默认值为1m,可单独在server,location中设置
client_max_body_size  100m;
#请求头的超时时间,默认60s
client_header_timeout 60s;
client_body_timeout 60s;
#配置缓冲区,线上环境优化重点
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 2 4k;
proxy_busy_buffers_size 4k;
proxy_max_temp_file_size 20M;
proxy_temp_file_write_size 8k;
proxy_connect_timeout 60s;
proxy_read_timeout 1m;
proxy_send_timeout 1m;

gzip on;
#gzip_static on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
#gzip_types text/plain application/x-javascript text/css application/xml;
gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg  image/gif image/png;
gzip_vary on;
gzip_proxied   expired no-cache no-store private auth;
gzip_disable   "MSIE [1-6]\.";

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

xxx.conf文件内容

upstream http_res{
        server xxx.18.xxx.xxx:80;
        server xxx.18.xxx.xxx:81;
        server xxx.18.xxx.xxx:82;
}
server {
        listen       80;
        server_name xxx.xxx.251.xxx;
        #location / {
         #   root   html;
          #  index  index.html index.htm;
        #}
         location  /audit/ {
          alias /data/web/audit/xxx_web/;
          index index.html /data/web/audit/xxx_web/index.html;
        }
	location /minio/ {
		#proxy_http_version 1.1;
    		#proxy_set_header Upgrade $http_upgrade;
    		#proxy_set_header Connection "upgrade";
	        #proxy_set_header   Host 172.19.57.11;  		
                proxy_pass http://127.0.0.1:9001/;
	}

	 location /api2/ {
			proxy_pass http://http_res;
			proxy_http_version 1.1;
			proxy_set_header   Host $http_host;
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
	}
	 location /api/ {
			proxy_pass http://http_res;
			proxy_http_version 1.1;
			proxy_set_header   Host $http_host;
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
	}
 }

nginx.conf文件内容

user  root;
# 跟服务器核数相同
worker_processes  8;

events {
    worker_connections  4096;
    #worker_rlimit_nofile 65535;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    include config/base.conf;
    server_names_hash_bucket_size 64;
    charset utf-8;
    send_timeout 120s;
    # 读取vhost下所有.conf文件
    include vhost/*.conf;
}

启动、停止、重启

[root@zonghe02 conf]# /data/nginx24/sbin/nginx
[root@zonghe02 conf]# /data/nginx24/sbin/nginx -s stop
[root@zonghe02 conf]# /data/nginx24/sbin/nginx -s reload
### Nginx 生产环境配置最佳实践 对于生产环境配置 Nginx 服务前端应用程序,有若干关键因素需要考虑以确保性能、安全性和可靠性。 #### 配置文件结构优化 为了提高可维护性,建议采用模块化的方式管理 Nginx配置文件。可以创建独立的配置片段来处理不同的功能需求,比如 SSL 设置、缓存策略等[^1]。 #### 性能调优参数设置 合理调整 worker_processes 和 worker_connections 参数能够显著提升服务器并发处理能力。通常推荐将 worker_processes 设定为 CPU 核心数,并根据预期的最大连接数适当增加 worker_connections 值。 ```nginx worker_processes auto; events { worker_connections 1024; } ``` #### 安全加固措施 启用 HTTPS 是保护数据传输安全的基础手段之一。通过 Let's Encrypt 获取免费证书并定期更新。此外,应关闭不必要的 HTTP 方法(如 DELETE, PUT),限制 IP 访问以及实施严格的 Content Security Policy (CSP)。 #### 日志记录与监控 开启详细的访问日志和错误日志有助于后续排查问题。同时集成 Prometheus 或其他监控工具实时跟踪 Nginx 运行状态,及时发现潜在风险点。 #### 缓存机制运用 利用 FastCGI Cache 或者 Proxy Cache 可有效减轻后端压力,加快静态资源加载速度。针对不同类型的请求制定差异化的缓存策略,例如对 API 接口不进行缓存而对 HTML 页面则长时间保存副本。 #### 负载均衡方案设计 当单台 Nginx 实例无法满足业务流量时,则需引入负载均衡器分发请求至多台 Web Server 上面。可以选择 upstream 模块配合 ip_hash 方式实现会话保持功能,在保证高可用性的前提下提供更好的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值