在 Nginx 上进行安装,并将其配置为指定目录、启用 SSL、以及配置 conf.d 文件夹,按照以下步骤进行(centos):
安装 Nginx
首先,确保你的 CentOS 系统已经更新,运行以下命令来安装 Nginx:
sudo yum update -y
sudo yum install -y epel-release
sudo yum install -y nginx安装指定目录
指定目录安装 Nginx(例如,你希望自定义安装位置),可以从源码进行编译。
安装编译依赖
sudo yum install -y pcre-devel zlib-devel openssl-devel gcc下载 Nginx 源码
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0配置指定目录进行安装:
#命令
./configure --prefix=/path/to/your/custom/directory
#示例
mkdir -p /home/apps/nginx
./configure --prefix=/home/apps/nginx --with-http_ssl_module编译并安装:
make
sudo make install优化配置
配置 conf.d 文件夹 conf.d 目录用于存放额外的配置文件。默认情况下,CentOS 中的 Nginx 配置文件位于 /etc/nginx/nginx.conf,你可以在 http 块中引用该目录。
#在指定位置建立目录
mkdir /home/apps/nginx/conf.d
chmod 777 /home/apps/nginx/conf.d
#编辑conf文件
cd /home/apps/nginx/conf
#在20行后添加(按 : 后 输入set number后回车)
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for" '
'upstream: $upstream_addr, response_time: $upstream_response_time '
'request_time: $request_time, upstream_status: $upstream_status';
include /home/apps/nginx/conf.d/*.conf;配置 https 模块 查看nginx是否安装https模块
#查看https模块是否安装
cd /home/apps/nginx/
./nginx -V
#正确输出
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/home/apps/nginx/ --with-http_ssl_module代理测试
http反向代理测试
#编写代理项目
cd /home/apps/nginx/conf.d/
vi test.conf
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://127.0.0.1; # 将请求转发到后端服务器
proxy_set_header Host $host; # 设置头部,确保后端能正确接收请求
proxy_set_header X-Real-IP $remote_addr; # 传递客户端真实 IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 转发客户端 IP
proxy_set_header X-Forwarded-Proto $scheme; # 设置协议
}
}
#查看是否正常
cd /home/apps/nginx/sbin/
./nginx -t
nginx: the configuration file /home/apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/apps/nginx/conf/nginx.conf test is successful
#启动nginx
./nginxhttps反向代理测试
#这里不做实际展示,配置完记得重启
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/nginx/ssl/yourdomain.crt;
ssl_certificate_key /etc/nginx/ssl/yourdomain.key;
location / {
proxy_pass http://127.0.0.1:8080;
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;
}
}
评论