Nginx新手教程:从安装到反向代理配置

Nginx是高性能HTTP和反向代理服务器,轻量稳定,适用于网站搭建、负载均衡等场景。安装分Ubuntu/Debian(`sudo apt install nginx`)和CentOS/RHEL(`sudo yum install nginx`),验证用`nginx -v`。启动服务(`sudo systemctl start nginx`)并设自启(`sudo systemctl enable nginx`),管理命令含启停、重启及重载配置(`reload`)。 核心反向代理配置:在`/etc/nginx/conf.d/`新建站点配置文件(如`myapp.conf`),示例配置:`server`监听80端口,`server_name`设域名/IP,`location /`通过`proxy_pass`转发至后端端口(如`127.0.0.1:3000`),并通过`proxy_set_header`传递Host和真实IP。配置后用`nginx -t`检查语法,`reload`生效,测试访问后端内容。 注意事项:开放防火墙80/443端口,确保后端服务运行,`proxy_pass`需以`http://`/`https

阅读全文