nginx小练习

张开发
2026/4/10 20:04:29 15 分钟阅读

分享文章

nginx小练习
本次活动利用nginx搭建静态页面web服务器了解反向代理。nginx简介Nginx 是高性能的 HTTP 和反向代理的web服务器 专为性能优化而开发处理高并发能力强大能支持高达 50,000 个并发连接数且占有内存少百度、京东、新浪、网易、腾讯、淘宝等网站都使用了nginx。Nginx 可以作为静态页面的 web 服务器同时还支持 CGI 协议的动态语言比如 perl、php 等但是不支持 java。Java 程序只能通过与 tomcat 配合完成。一、安装nginx1.下载并解压nginx压缩包#下载nginx官方包 wget https://nginx.org/download/nginx-1.27.3.tar.gz #解压 tar -zxvf nginx-1.27.3.tar.gz #缺少wget命令 yum install -y wget #倘若缺失yum源则参考下方yum缺失解决方案2.进入nginx目录运行配置#进入解压后的文件 cd nginx-1.27.3 #运行configure文件 ./configure若出现类似于如下缺少环境库包等报错则下载相应环境库包#安装环境 yum install -y gcc gcc-c autoconf automake make pcre pcre-devel zlib zlib-devel #若yum源缺失 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo sed -i -e /mirrors.cloud.aliyuncs.com/d -e /mirrors.aliyuncs.com/d /etc/yum.repos.d/CentOS-Base.repo yum clean all yum makecache下载完成后继续运行./configure编译安装运行nginx#编译安装 make make install #进入nginx核心可执行文件目录 cd /usr/local/nginx/sbin #启动nginx ./nginx #创建一个符号链接简化启动流程 ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx启动停止重启命令#启动 ./nginx #关机 ./nginx -s stop #重启 ./nginx -s reload启动浏览器后访问192.168.20.21:80(根据自己的ip修改)自此nginx安装完成二、配置静态页面web1. 拉入2048.tar.gz和VikingWars.tar.gz文件并解压#移除目录下文件 mv /usr/local/nginx/html/* /tmp #解压文件 tar -zxvf VikingWars.tar.gz -C /usr/local/nginx/html/ tar -zxvf 2048.tar.gz -C /usr/local/nginx/html/2. 防止权限不足chmod -R 755 /usr/local/nginx/html/3. 修改配置文件进入配置目录cd /usr/local/nginx/conf/保险起见先备份配置文件#备份文件 cp nginx.conf nginx.conf.bak#配置文件 vi nginx.conf #配置如下 user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 主服务器 (VikingWars游戏 - 80端口) server { listen 80; server_name localhost; # han游戏 - 主站点 location / { root /usr/local/nginx/html/VikingWars; index index.html; try_files $uri $uri/ 404; } # 2048游戏反向代理 location /2048/ { proxy_pass http://localhost:8080/; 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; } error_page 500 502 503 504 /50x.html; location /50x.html { root html; } } # 2048游戏专用服务器 (8080端口) server { listen 8080; server_name localhost; location / { root /usr/local/nginx/html/2048; index index.html; try_files $uri $uri/ 404; } } }4. 测试配置nginx -t #输出 #nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok #nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful5.重启nginx服务nginx -s reload三、成果展示1.访问http:IP地址:802.访问http://IP地址:8080端口 和 http://IP地址:80/2048/:

更多文章