大数据Nginx之——Nginx安装与使用(详细)
目录1、安装编译依赖2、下载和解压 Nginx 源码3、配置、编译与安装4、启动 Nginx 并验证5、设置 systemd 服务推荐6、启停命令7、反向代理案例1、安装编译依赖sudo yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel2、下载和解压 Nginx 源码cd /opt/modulesudo wget https://nginx.org/download/nginx-1.26.2.tar.gzsudo tar -zxvf nginx-1.26.2.tar.gzcd nginx-1.26.23、配置、编译与安装mkdir -p /opt/module/nginx# 配置编译选项./configure --prefix/opt/module/nginx \--with-http_ssl_module \--with-http_v2_module \--with-http_realip_module \--with-http_stub_status_modulemake -j $(nproc)# 安装到系统sudo make install4、启动 Nginx 并验证# 启动 Nginxsudo /opt/module/nginx/sbin/nginx# 验证 Nginx 进程是否存在ps aux | grep nginx# 验证端口是否正常监听curl -I http://localhost浏览器访问http://IP停止sudo /opt/module/nginx/sbin/nginx -s quit5、设置 systemd 服务推荐sudo vim /etc/systemd/system/nginx.service[Unit]DescriptionThe NGINX HTTP and reverse proxy serverAfternetwork.target remote-fs.target nss-lookup.target[Service]TypeforkingPIDFile/usr/local/nginx/logs/nginx.pidExecStartPre/usr/local/nginx/sbin/nginx -tExecStart/usr/local/nginx/sbin/nginxExecReload/usr/local/nginx/sbin/nginx -s reloadExecStop/usr/local/nginx/sbin/nginx -s quitKillModemixedTimeoutStopSec5PrivateTmptrue[Install]WantedBymulti-user.target6、启停命令sudo systemctl daemon-reloadsudo systemctl enable nginx.servicesudo systemctl start nginx.servicesudo systemctl status nginx.service7、反向代理案例Nginx 通过 server 块定义虚拟主机通过 location 块匹配请求路径再通过 proxy_pass 将请求转发到后端服务。浏览器请求 → Nginx (80端口) → proxy_pass → 后端服务 (如 localhost:8080)server {listen 80;server_name localhost;# 代理到 Java 后端 APIlocation /api/ {proxy_pass http://localhost:8080/api/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;}# 代理到前端开发服务器location /app/ {proxy_pass http://localhost:3000/app/;proxy_set_header Host $host;}# 默认静态文件location / {root /usr/local/nginx/html;index index.html;}}