NGINX服务(六)
nginx 服务nginx 定位web 服务器、反向代理服务器、缓存服务器、邮件服务器架构master【程序管理】-worker【处理请求的】安装方式二进制包、源码包【注意编译环境】配置结构全局、event、http [server【location】]server 重要属性listen(指定监听端口)、root(设定网站根目录)、index(设定默认首页)、alias(别名映射)location 重要属性root(拼接访问路径与本地目录)、index(指定默认访问文件)、alias(精准匹配替换访问路径)访问流量逻辑:客户端请求→监听端口匹配 server 虚拟主机→匹配对应 location→按规则转发 / 反向代理 / 返回静态资源→响应数据回传给客户端。nginx 功能多网站(配置多虚拟主机也就是多server块)、alias 和 root (实现网站目录路径映射)、日志(访问日志错误日志记录)、状态页面(stub_status查看运行状态)、身份认证(账号密码登录校验)、https(SSL证书加密传输 )[rewrite(URL地址重写跳转)]、防盗链(通过Referer拦截非法盗链)nginx实践nginx动静分离注client(客户端) 12主机proxy Server代理 Ubuntu13API server动态 ubuntu 16Static server静态 openuler1.OpenEuler实现静态分离服务配置环境echo Static Web Server /usr/share/nginx/html/index.htmlcat /etc/nginx/default.d/simple.conflocation / {add_header X-Host $host;}启动nginx服务systemctl start nginx关闭防火墙systemctl stop firewalld测试效果curl localhost2.ubuntu16动态配置定制python的web服务from http.server import BaseHTTPRequestHandler, HTTPServer class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): def do_GET(self): # 获取请求的URL路径 url_path self.path # 获取请求头中的Host信息 host self.headers.get(Host) # 设置响应状态码 self.send_response(200) # 设置响应头 self.send_header(Content-type, text/plain; charsetutf-8) self.end_headers() # 构造响应内容 response_content fAPI Server: {url_path}, Host Header: {host}\n # 发送响应内容 self.wfile.write(response_content.encode(utf-8)) def run(server_classHTTPServer, handler_classSimpleHTTPRequestHandler, port8080): server_address (, port) httpd server_class(server_address, handler_class) print(fStarting httpd server on port {port}) httpd.serve_forever() if __name__ __main__: run(port8080)启动python服务python3 simple_http_server.py /dev/null 21 测试curl 10.0.0.16:8080/nihao实现代理配置ubuntu13server { listen 80 default_server; server_name sswang.magedu.com; root /data/server/nginx/web1; location /api { proxy_pass http://10.0.0.16:8080; proxy_set_header Host api.magedu.com; } location /static { rewrite ^/static(.*)$ /index.html break; proxy_pass http://10.0.0.14; proxy_set_header Host static.magedu.com; } location /static1/ { proxy_pass http://10.0.0.14/; proxy_set_header Host static.magedu.com; } }重启服务systemctl restart nginx测试效果nginx -t systemctl restart nginxcurl -H sswang.magedu.com 10.0.0.13curl -H sswang.magedu.com 10.0.0.13/apicurl -H sswang.magedu.com 10.0.0.13/staticcurl -H sswang.magedu.com 10.0.0.13/static1curl -H sswang.magedu.com 10.0.0.13/static1 -Lcurl -H sswang.magedu.com 10.0.0.13/static1 -I1客户端测试rocky12curl -H sswang.magedu.com 10.0.0.13/static1 -Lcurl -H sswang.magedu.com 10.0.0.13/static1 -INginxNodeNPM 实验实验名称Nginx 反向代理对接 Node.js NPM 项目综合演示 实验主机代理端Rocky 10-15IP 10.0.0.15部署 Nginx服务端Rocky 10-12IP 10.0.0.12部署 NodeNPM二、后端 10.0.0.12 Node 服务端安装 Node.js 和 NPM 环境 执行命令dnf install nodejs npm -ynode -vnpm -v创建实验项目目录并初始化 NPM 项目 执行命令mkdir -p /opt/nginx-node-democd /opt/nginx-node-demo npm init -y编写 Node 演示 Web 服务 执行命令创建服务文件vim index.js放行端口、关闭安全拦截 执行命令firewall-cmd --add-port3000/tcp --permanentfirewall-cmd --reloadsetenforce 0启动 Node 后端服务 执行命令node index.js三、前端 10.0.0.15 Nginx 代理端安装 Nginx 服务 执行命令dnf install nginx -y创建反向代理配置文件 执行命令vim /etc/nginx/conf.d/node-proxy.confserver { listen 80; server_name _; location / { proxy_pass http://10.0.0.12:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }校验配置并重启 Nginx 执行命令nginx -tsystemctl restart nginxsystemctl enable nginx放行 Nginx 80 端口 执行命令firewall-cmd --add-servicehttp --permanentfirewall-cmd --reload四、连通验证步骤在 10.0.0.15 代理主机执行测试访问 curl localhost浏览器直接访问 http://10.0.0.15 成功看到 Node 后端返回文字代表整个实验全部完成五、NPM 演示在后端项目目录执行 NPM 包管理操作 npm install express --save 实现 Express 框架的 Node Web 项目部署演示nginx反向代理实验部署python环境apt -y install python3 python3-pip nginx build-essential python3-dev python3-venv创建python虚拟环境cd /data/lspython3 -m venv myprojectenvsource myprojectenv/bin/activate安装django环境pip config set global.index-url https://pypi.doubanio.com/simplepip config set install.trusted-host pypi.doubanio.compip install djangopip install uwsgi安装django项目django-admin startproject myproject配置django项目cd myprojectlsvim myproject/settings.py数据库环境初始化python manage.py makemigrationspython manage.py migrate创建登录用户python manage.py createsuperuser定制配置vim myproject/settings.py收集静态文件python manage.py collectstatic在项目根目录下创建一个uwsgi.ini文件vim uwsgi.ini赋予权限chown -R www-data:www-data /data/myprojectchmod -R 755 /data/myproject创建一个名为uwsgi.service文件vim /etc/systemd/system/uwsgi.service启动服务systemctl daemon-reloadsystemctl start uwsgi.servicesystemctl is-active uwsgi.service定制nginx配置vim /etc/nginx/conf.d/myproject.conf检测配置文件nginx -t启动项目systemctl start nginx.servicesystemctl is-active nginx.service测试效果