青海网站建设公司电话,建网站软件下载,武进区城乡建设局网站,网络营销网页制作源代码你好呀#xff0c;我是赵兴晨#xff0c;文科程序员。
最近一段时间#xff0c;我一直在和大家一起探讨Nginx的相关话题。期间#xff0c;我收到了很多小伙伴的私信#xff0c;他们好奇地问我#xff1a;在生产环境中#xff0c;Nginx应该如何配置#xff1f;
他们在…
你好呀我是赵兴晨文科程序员。
最近一段时间我一直在和大家一起探讨Nginx的相关话题。期间我收到了很多小伙伴的私信他们好奇地问我在生产环境中Nginx应该如何配置
他们在日常的开发工作中大多只接触到了增删改查等基础操作对于Nginx的部署并不熟悉。
今天就让我们来一起深入了解一下打破“Java程序员不能部署Nginx”的刻板印象。我们来动手实践看看如何将Nginx部署到生产环境中。
文章篇幅过长建议先收藏后再细品。
实验环境Centos7.9 4核8g SSD 100G
步骤1 安装依赖
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel make wget• gcc: 这是GNU编译器集合GNU Compiler Collection用于编译C、C等语言编写的源代码。它是开发新程序和编译现有程序的核心工具 • pcre: PCREPerl Compatible Regular Expressions是一个Perl库用于正则表达式模式匹配。它提供了对正则表达式的实现这在文本处理和字符串分析中非常有用。 • pcre-devel: 这是PCRE的开发版本包含了头文件和库文件用于开发需要使用PCRE功能的程序。 • zlib: zlib是一个提供数据压缩功能的库。它支持DEFLATE压缩算法广泛用于网络传输和文件压缩。 • zlib-devel: 与zlib类似但这是开发版本包含了开发新软件时所需的头文件和开发库。 • openssl: OpenSSL是一个强大的开源工具包用于实现安全套接层SSL和传输层安全TLS协议以及加密、解密、证书生成等安全功能。 • openssl-devel: 这是OpenSSL的开发版本包含了用于开发支持SSL/TLS功能的应用程序的开发库和头文件。 • make: Make是一个构建自动化工具它自动编译和链接程序根据依赖关系来确定哪些部分需要重新编译。它通常用于大型项目其中源代码分布在多个文件中。
步骤2 创建nginx用户
# 创建名为nginx的新用户组
groupadd nginx# 创建名为nginx的用户 指定用户的主目录为/home/nginx 并添加到nginx用户组
useradd nginx -m -d /home/nginx -g nginx# 设置用户 nginx的密码为 nginx 生产环境可以设置的再复杂一些
echo nginx:nginx|chpasswd步骤3 创建nginx相关目录
# 创建相关目录
mkdir -p /usr/local/nginx/{cache,log,conf/conf.d}# 授权/usr/local/nginx目录 所属nginx用户
chown -R nginx:nginx /usr/local/nginx步骤4 下载nginx包并解压
# 创建目录 用于保存下载的nginx压缩包
mkdir /opt/software/# 切换到 /opt/software/ 目录
cd /opt/software/# 下载 nginx-1.20.1.tar.gz
wget http://nginx.org/download/nginx-1.20.1.tar.gz# 解压
tar -zxvf nginx-1.20.1.tar.gz步骤5 编译并安装 nginx
# 切换到刚解压的 nginx-1.20.1目录
cd nginx-1.20.1# 设置预编译
./configure \
--prefix/usr/local/nginx \
--sbin-path/usr/sbin/nginx \
--conf-path/usr/local/nginx/conf/nginx.conf \
--error-log-path/usr/local/nginx/log/error.log \
--http-log-path/usr/local/nginx/log/access.log \
--pid-path/usr/local/nginx/nginx.pid \
--lock-path/usr/local/nginx/nginx.lock \
--http-client-body-temp-path/usr/local/nginx/cache/client_temp \
--http-proxy-temp-path/usr/local/nginx/cache/proxy_temp \
--http-fastcgi-temp-path/usr/local/nginx/cache/fastcgi_temp \
--http-uwsgi-temp-path/usr/local/nginx/cache/uwsgi_temp \
--http-scgi-temp-path/usr/local/nginx/cache/scgi_temp \
--usernginx \
--groupnginx \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module # 开始编译并安装
make make install以下是对预编译配置相关模块的解释都是常用的nginx模块按需安装即可
–prefix/usr/local/nginx 设置Nginx安装的基本目录。
–sbin-path/usr/sbin/nginx 指定Nginx二进制文件的安装路径。
–conf-path/usr/local/nginx/conf/nginx.conf 指定Nginx主配置文件的路径。
–error-log-path/usr/local/nginx/log/error.log 指定错误日志文件的存放路径。
–http-log-path/usr/local/nginx/log/access.log 指定HTTP访问日志文件的存放路径。
–pid-path/usr/local/nginx/nginx.pid 指定存放Nginx进程ID文件的路径。
–lock-path/usr/local/nginx/nginx.lock 指定存放Nginx锁定文件的路径。
–http-client-body-temp-path/usr/local/nginx/cache/client_temp 指定存放客户端请求体的临时文件目录。
–http-proxy-temp-path/usr/local/nginx/cache/proxy_temp 指定存放代理请求的临时文件目录。
–http-fastcgi-temp-path/usr/local/nginx/cache/fastcgi_temp 指定FastCGI进程的临时文件目录。
–http-uwsgi-temp-path/usr/local/nginx/cache/uwsgi_temp 指定uWSGI进程的临时文件目录。
–http-scgi-temp-path/usr/local/nginx/cache/scgi_temp 指定SCGI进程的临时文件目录。
–usernginx 指定运行Nginx进程的用户名。
–groupnginx 指定运行Nginx进程的用户组名。
–with-file-aio 启用文件异步I/O支持。
–with-threads 启用线程支持。
–with-http_addition_module 启用HTTP addition模块。
–with-http_auth_request_module 启用HTTP auth request模块。
–with-http_dav_module 启用HTTP DAV模块。
–with-http_flv_module 启用HTTP FLV模块。
–with-http_gunzip_module 启用HTTP gunzip模块。
–with-http_gzip_static_module 启用HTTP gzip static模块。
–with-http_mp4_module 启用HTTP MP4模块。
–with-http_random_index_module 启用HTTP random index模块。
–with-http_realip_module 启用HTTP realip模块。
–with-http_secure_link_module 启用HTTP secure link模块。
–with-http_slice_module 启用HTTP slice模块。
–with-http_ssl_module 启用HTTP SSL模块。
–with-http_stub_status_module 启用HTTP stub status模块。
–with-http_sub_module 启用HTTP sub模块。
–with-http_v2_module 启用HTTP/2模块。
–with-mail 启用邮件支持。
–with-mail_ssl_module 启用邮件SSL模块。
–with-stream 启用RTMP或WebRTC流模块。
–with-stream_realip_module 启用流realip模块。
–with-stream_ssl_module 启用流SSL模块。
–with-stream_ssl_preread_module 启用流SSL预读取模块。
步骤6 修改nginx配置文件并调优整段复制下面的内容到终端回车。
根据自身业务需求去调整配置文件相关配置均以注释。
cat EOF | tee /usr/local/nginx/conf/nginx.conf
# 配置 worker 进程所属用户,用户组
user nginx nginx;# 配置 worker 进程数量为避免 cpu 切换损耗配置和系统内核数一样即可,或者 auto
worker_processes auto;#nginx pid文件
pid /usr/local/nginx/nginx.pid;# 配置 cpu 亲和,auto 代表自动绑定
worker_cpu_affinity auto;# nginx 进程打开文件描述符数目此值覆盖 ulimit -n 的值最好与优化后的ulimit -n的值相同。
worker_rlimit_nofile 1024000;events {# 用这个模型来高效处理异步事件use epoll;# 设置为 on worker 进程轮流接受新链接,官方推荐设置为 off.高负载的情况下设置为 on.accept_mutex on;# worker进程是否同时接受连接所有新请求。默认为off表示一次只接受一个新的请求。官方推荐 offmulti_accept on;# 配置 一个 woker 进程处理的连接数;nginx最大的连接数Maxclient work_processes * worker_connectionsworker_connections 65535;
}http { include mime.types;# 关闭日志access_log off;# 隐藏响应头中的有关操作系统和web serverNginx版本号的信息这样对于安全性是有好处的。server_tokens off;sendfile on;# 设置为非零值时可限制单个 sendfile() 调用时传输的数据量。如果没有限制一个快速 连接可能会完全占用工作进程。#sendfile_max_chunk 5m;# tcp_nopush 和 tcp_nodeny 可以一起生效# 等数据包累计到一定大小发送启用 sendfile 生效tcp_nopush on;# 该选项仅在连接转换到 keep-alive 长连接状态时启用。让 tcp 尽快发包。tcp_nodelay on;# 为了尽快释放连接,可以设置小点. 15 至 30keepalive_timeout 30;gzip on;# 在响应头中增加Vary: Accept-Encoding#gzip_vary on;# gzip压缩级别1-9数字越大压缩效果越好压缩时间也就越长CPU越高#gzip_comp_level 5;# 申请内存时大小如果源文件 9k超过了 8K那会申请 16*8K。#gzip_buffers 8 128k;#gzip_min_length 5K;#gzip_proxied any;#gzip_disable msie6;#gzip_http_version 1.1;# 文本js、text、css、xml、json压缩比较好图片已经进行过压缩在压缩效果不是很明显还浪费 cpu#gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xmlrss application/rssxml application/atomxml image/svgxml;# 安全相关 header#add_header X-Frame-Options SAMEORIGIN always;#add_header Feature-Policy autoplay none; camera none always;#add_header X-XSS-Protection 1; modeblock always;#add_header X-Content-Type-Options nosniff always;#add_header Referrer-Policy no-referrer-when-downgrade always;#add_header Content-Security-Policy default-src self http: https: data: blob: unsafe-inline always;#add_header Strict-Transport-Security max-age31536000; includeSubDomains; preload always;include /usr/local/nginx/conf/conf.d/*.conf;
}
EOF步骤7 设置nginx开机自启
以下内容整段复制到终端回车即可。
cat EOF |tee /etc/systemd/system/nginx.service
[Unit]
DescriptionThe NGINX HTTP and reverse proxy server
Aftersyslog.target network-online.target remote-fs.target nss-lookup.target
Wantsnetwork-online.target
[Service]
Typeforking
PIDFile/usr/local/nginx/nginx.pid
ExecStartPre/usr/sbin/nginx -t
ExecStart/usr/sbin/nginx
ExecReload/usr/sbin/nginx -s reload
ExecStop/bin/kill -s QUIT $MAINPID
PrivateTmptrue
[Install]
WantedBymulti-user.target
EOF设置开机自启并启动nginx
systemctl daemon-reload systemctl enable --now nginx查看nginx启动状态
systemctl status nginx到这nginx部署结束nginx配置建议写在 /usr/local/nginx/conf/conf.d 以 .conf 结尾的文件即可
补充几个命令
# nginx 语法检测 用于修改nginx配置文件之后 验证配置格式是否正确
nginx -t# nginx 加载配置文件(推荐使用)
systemctl reload nginx # 或者
nginx -s reload# 停止nginx服务
systemctl stop nginx # 重启nginx服务
systemctl restart nginx # 启动nginx服务
systemctl rtart nginx 最后我想说的是点赞和分享不仅仅是一种美德更是对未来美好生活的投资。愿每一个点在看的朋友未来都能收获满满的幸福和成功
你好我是赵兴晨一名文科程序员。我期待在文章下方看到你的留言让我们一起交流共同进步。