Nginx 部署文档
off999 2025-01-12 17:40 15 浏览 0 评论
一、文档介绍
此文档旨在规范服务器上 Nginx 部署步骤,标准化操作步骤,为后续标准运维提供支撑。
二、部署说明
- 操作系统:Linux(CentOS 7.6)
- 安装包版本:Nginx 1.26.2
三、下载
官网下载地址:https://nginx.org/download/
cd /usr/local/src
wget https://nginx.org/download/nginx-1.26.2.tar.gz
四、安装依赖包
yum install -y openssl openssl-devel pcre pcre-devel libxml2 libxml2-devel libxslt libxslt-devel gd gd-devel pcre pcre-devel perl-ExtUtils-Embed
五、安装
cd /usr/local/src
tar zxf nginx-1.26.2.tar.gz
cd nginx-1.26.2
# 解决无法找到 openssl 问题
sed '/ngx_feature_libs/s#R/usr/local/lib #R/usr/local/lib64 #g' auto/lib/openssl/conf
# 编译
./configure --prefix=/usr/local/nginx --with-compat --with-debug --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_perl_module=dynamic --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-http_mp4_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads
make -j2
# 安装
make install
六、创建目录
mkdir -p /data/nginx_data/
mkdir -p /data/logs/nginx
cd /usr/local/nginx
mv conf /data/nginx_data/
ln -s /data/nginx_data/conf conf
mkdir -p conf/conf.d
rm -rf logs
ln -s /data/logs/nginx logs
mkdir -p /data/nginx_data/certs
ln -s /data/nginx_data/certs certs
七、配置环境变量
cat <<"EOF" | tee -a /etc/profile
# nginx
export NGINX_HOME=/usr/local/nginx
export PATH=$NGINX_HOME/sbin:$PATH
EOF
source /etc/profile
八、修改配置文件
cd /usr/local/nginx/conf
cat <<EOF | tee nginx.conf
user root ;
worker_processes auto;
worker_cpu_affinity auto;
events {
use epoll;
worker_connections 65535;
accept_mutex on;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
underscores_in_headers on;
server_tokens off;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_time '
'$upstream_response_time $upstream_addr $upstream_status';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
proxy_buffer_size 128k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 128k;
keepalive_timeout 65;
gzip on;
gzip_proxied any;
#gzip_min_length 1k;
gzip_comp_level 6;
#gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_types text/plain text/css text/xml text/javascript application/xml application/javascript application/json application/octet-stream image/jpeg image/gif image/png;
include conf.d/*.conf;
}
EOF
九、创建服务
cd /data/nginx_data
cat <<EOF | tee nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf
ExecStop=/usr/local/nginx/sbin/nginx -s stop -c /usr/local/nginx/conf/nginx.conf
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
cp nginx.service /usr/lib/systemd/system/
十、启动服务
systemctl daemon-reload
systemctl enable nginx --now
systemctl status nginx
十一、添加配置
代理或前端配置文件目录:/usr/local/nginx/conf/conf.d。例如:
(一) 前端
server {
listen 80;
location / {
alias /usr/share/nginx/html/dist/;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
}
(二) HTTP 代理
server {
listen 443 ssl;
listen 80;
server_name api.rucjohn.tech;
index index.html index.htm index.php;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSV1.2;
ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
ssl_certificate "/usr/local/nginx/certs/server.crt";
ssl_certificate_key "/usr/local/nginx/certs/server.key";
location ^~ /api/v1/simple {
proxy_pass http://127.0.0.243;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
(三) TCP 代理
upstream mysql {
hash $remote_addr consistent;
server 127.0.0.240:3306;
}
server {
listen 13306 so_keepalive=on;
proxy_pass mysql;
}
相关推荐
- 全网第一个讲清楚CPK如何计算的Step by stepExcel和Python同时实现
-
在网上搜索CPK的计算方法,几乎全是照搬教材的公式,在实际工作做作用不大,甚至误导人。比如这个又比如这个:CPK=min((X-LSL/3s),(USL-X/3s))还有这个,很规范的公式,也很清晰很...
- [R语言] R语言快速入门教程(r语言基础操作)
-
本文主要是为了从零开始学习和理解R语言,简要介绍了该语言的最重要部分,以快速入门。主要参考文章:R-TutorialR语言程序的编写需要安装R或RStudio,通常是在RStudio中键入代码。但是R...
- Python第123题:计算直角三角形底边斜边【PythonTip题库300题】
-
1、编程试题:编写一个程序,找出已知面积和高的直角三角形的另外两边(底边及斜边)。定义函数find_missing_sides(),有两个参数:area(面积)和height(高)。在函数内,计算另外...
- Tensor:Pytorch神经网络界的Numpy
-
TensorTensor,它可以是0维、一维以及多维的数组,你可以将它看作为神经网络界的Numpy,它与Numpy相似,二者可以共享内存,且之间的转换非常方便。但它们也不相同,最大的区别就是Numpy...
- python多进程编程(python多进程进程池)
-
forkwindows中是没有fork函数的,一开始直接在Windows中测试,直接报错importosimporttimeret=os.fork()ifret==0:...
- 原来Python的协程有2种实现方式(python协程模型)
-
什么是协程在Python中,协程(Coroutine)是一种轻量级的并发编程方式,可以通过协作式多任务来实现高效的并发执行。协程是一种特殊的生成器函数,通过使用yield关键字来挂起函数的执行...
- ob混淆加密解密,新版大众点评加密解密
-
1目标:新版大众点评接口参数_token加密解密数据获取:所有教育培训机构联系方式获取难点:objs混淆2打开大众点评网站,点击教育全部,打开页面,切换到mobile模式,才能找到接口。打开开发者工具...
- python并发编程-同步锁(python并发和并行)
-
需要注意的点:1.线程抢的是GIL锁,GIL锁相当于执行权限,拿到执行权限后才能拿到互斥锁Lock,其他线程也可以抢到GIL,但如果发现Lock仍然没有被释放则阻塞,即便是拿到执行权限GIL也要立刻...
- 10分钟学会Python基础知识(python基础讲解)
-
看完本文大概需要8分钟,看完后,仔细看下代码,认真回一下,函数基本知识就OK了。最好还是把代码敲一下。一、函数基础简单地说,一个函数就是一组Python语句的组合,它们可以在程序中运行一次或多次运行。...
- Python最常见的170道面试题全解析答案(二)
-
60.请写一个Python逻辑,计算一个文件中的大写字母数量答:withopen(‘A.txt’)asfs:count=0foriinfs.read():ifi.isupper...
- Python 如何通过 threading 模块实现多线程。
-
先熟悉下相关概念多线程是并发编程的一种方式,多线程在CPU密集型任务中无法充分利用多核性能,但在I/O操作(如文件读写、网络请求)等待期间,线程会释放GIL,此时其他线程可以运行。GIL是P...
- Python的设计模式单例模式(python 单例)
-
单例模式,简单的说就是确保只有一个实例,我们知道,通常情况下类其实可以有很多实例,我们这么来保证唯一呢,全局访问。如配置管理、数据库连接池、日志处理器等。classSingleton: ...
- 更安全的加密工具:bcrypt(bcrypt加密在线)
-
作为程序员在开发工作中经常会使用加密算法,比如,密码、敏感数据等。初学者经常使用md5等方式对数据进行加密,但是作为严谨开发的程序员,需要掌握一些相对安全的加密方式,今天给大家介绍下我我在工作中使用到...
- 一篇文章搞懂Python协程(python协程用法)
-
前引之前我们学习了线程、进程的概念,了解了在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位。按道理来说我们已经算是把cpu的利用率提高很多了。但是我们知道无论是创建多进程还是创建多线...
- Python开发必会的5个线程安全技巧
-
点赞、收藏、加关注,下次找我不迷路一、啥是线程安全?假设你开了一家包子铺,店里有个公共的蒸笼,里面放着刚蒸好的包子。现在有三个顾客同时来拿包子,要是每个人都随便伸手去拿,会不会出现混乱?比如第一个顾...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python进度条 (67)
- python吧 (67)
- python字典遍历 (54)
- python的for循环 (65)
- python格式化字符串 (61)
- python静态方法 (57)
- python列表切片 (59)
- python面向对象编程 (60)
- python 代码加密 (65)
- python串口编程 (60)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python多态 (60)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)