Linux部署Nginx(超详细)
Nginx官网:
1、将Nginx安装包下载到服务器
目录:/usr/local/
命令行下载
wget -c http://nginx.org/download/nginx-1.24.0.tar.gz
注意:如果没有wget命令,需要先安装
yum install -y wget
手动下载
2、安装Nginx相关依赖
yum install -y gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel
3、安装Nginx
3.1 解压安装包
tar -zxvf nginx-1.24.0.tar.gz
3.2 执行配置脚本
#进入nginx目录
cd /usr/local/nginx-1.24.0
#执行配置脚本,支持SSL和HTTP2 --prefix是指定安装目录
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
注意:如果遇到报错“./configure: error: C compiler cc is not found”,如下图
解决:
yum -y install gcc gcc-c++ autoconf automake make
3.3 编译安装
#对nginx编译和安装
make & make install
4、Nginx添加到环境变量
#回到主目录
cd ~
在.bashrc
文件末行添加以下内容
export PATH="/usr/local/nginx/sbin:$PATH"
执行以下命令使环境变量生效
source ~/.bashrc
5、设置Nginx开机自启
#进入etc目录
cd /etc
在rc.local
文件末行添加以下内容
/usr/local/nginx/sbin/nginx
6、Nginx常用命令
默认启动
nginx
以指定配置启动
nginx -c /usr/local/nginx/conf/nginx.conf
强制停止
nginx -s stop
优雅停止(等最后一次交互执行完毕再停止)
nginx -s quit
重启
nginx -s reload
检查配置文件
nginx -t
查看版本信息
nginx -v
查看详细版本信息
nginx -V
7、常用配置
7.1 前后端分离(作为CDN源站)
worker_processes auto; # 根据 CPU 内核数自动设置工作进程数
pid /var/run/nginx.pid;
events {
worker_connections 2048; # 每个工作进程的最大连接数调整为2048
}
http {
include mime.types; # 包含 MIME 类型配置文件
default_type application/octet-stream; # 默认 MIME 类型设置为二进制流
client_max_body_size 30M;
client_body_timeout 120;
client_header_timeout 120;
keepalive_timeout 120;
server {
listen 8066; # 前端项目端口
server_name 123.123.123.123; # IP
root /www/wwwroot/test.com/dist; # Vue项目的根目录
index index.html; # 设置默认页面为index.html
location / {
try_files $uri $uri/ /index.html; # 尝试查找指定的文件,如果不存在则返回 index.html
}
# 前端代理后端API配置
location /api/ {
rewrite ^/api/(.*) /$1 break; # 去掉路径中的 /api
proxy_pass http://localhost:8080; # 反向代理到后端服务的本地8080端口
proxy_set_header Host $host; # 设置请求头中的Host字段
proxy_set_header X-Real-IP $remote_addr; # 设置请求头中的X-Real-IP字段
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 设置请求头中的X-Forwarded-For字段
proxy_set_header X-Forwarded-Proto $scheme; # 设置请求头中的X-Forwarded-Proto字段
}
}
access_log /var/log/nginx/access.log; # 访问日志文件路径
error_log /var/log/nginx/error.log; # 错误日志文件路径
}
7.2 同一个80/443端口转发不同服务+强制HTTPS
worker_processes auto; # 根据 CPU 内核数自动设置工作进程数
pid /var/run/nginx.pid;
events {
worker_connections 2048; # 每个工作进程的最大连接数调整为2048
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 30M;
client_body_timeout 120;
client_header_timeout 120;
keepalive_timeout 120;
#a.com转发到3000端口
server {
listen 80;
server_name a.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name a.com;
ssl_certificate /usr/local/nginx/acmesh/a.com/cert.pem;
ssl_certificate_key /usr/local/nginx/acmesh/a.com/key.pem;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
#b.com转发到6000端口
server {
listen 80;
server_name b.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name b.com;
ssl_certificate /usr/local/nginx/acmesh/b.com/cert.pem;
ssl_certificate_key /usr/local/nginx/acmesh/b.com/key.pem;
location / {
proxy_pass http://localhost:6000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
access_log /var/log/nginx/access.log; # 访问日志文件路径
error_log /var/log/nginx/error.log; # 错误日志文件路径
}
参考文章:
文章不足之处还请斧正!
本文By:NonNullPointer --2024/07/13