Nginx 简单使用配置
配置user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 365; server_tokens off; proxy_connect_timeout 300s; proxy_read_timeout 300s; proxy_send_timeout 300s; server { listen 80; # 监听的端口号 server_name 10.195.60.150; # 监听的主机名.域名 # # 业务一不告诉你 # # 1. 前端入口 (根路径) #匹配所有以 / 开头的请求路径例如http://example.com/、http://example.com/about 都会被这个 location 处理 location / { # 将请求的 URL 映射到服务器文件系统的实际路径例如:http://example.com/ → 对应文件系统路径 /usr/share/nginx/html/mkqznpt/ alias /usr/share/nginx/html/mkqznpt/; # 定义默认首页文件 index index.html; # # $uri首先检查请求的文件是否存在 # 访问 http://example.com/js/app.js → 检查 /usr/share/nginx/html/mkqznpt/js/app.js 是否存在 # $uri/如果文件不存在检查是否是目录 # 访问 http://example.com/about → 检查 /usr/share/nginx/html/mkqznpt/about/ 目录是否存在 # index.html如果前两个都不存在返回根目录下的 index.html # 这通常用于 单页应用SPA 的路由处理 # 访问 http://example.com/user/profile 如果文件不存在返回 index.html让前端路由接管 # try_files $uri $uri/ /index.html; } # 2. 后端接口 (直接透传保留 /rzfx 前缀) # # 匹配所有以 /rzfx/ 开头的请求路径 # http://example.com/rzfx/api/list - 匹配 # http://example.com/api/rzfx/list - 不匹配 # location /rzfx/ { # # 作用将原始请求的 Host 头传递给后端 # $host 变量优先取请求头中的 Host 字段,如果没有则使用 $http_host实际访问的域名/IP # 示例客户端访问https://api.example.com/rzfx/api/list 后端收到 Host: api.example.com proxy_set_header Host $host; #传递客户端真实 IP 地址,让后端能获取到客户端的真实 IP否则后端只能看到 Nginx 的 IP) proxy_set_header X-Real-IP $remote_addr; #自定义头部同样传递客户端 IP #某些后端框架可能从这个字段读取客户端 IP proxy_set_header REMOTE-HOST $remote_addr; #构建代理链记录所有经过的 IP,如果已有 X-Forwarded-For 头原有值, $remote_addr,如果没有直接设置为 $remote_addr proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #将请求反向代理到后端服务 proxy_pass http://127.0.0.1:9080; } # # 业务二menkong (门禁子系统) # location /menkong/ { alias /usr/share/nginx/html/menkong/menkong/; index index.html; try_files $uri $uri/ /index.html; } } }命令# 检查配置文件语法正确性修改后必须执行 nginx-t # 平滑重启Nginx不中断服务加载新配置 nginx-s reload # 启动/停止服务 systemctl start nginx systemctl stop nginx # 设置开机自启 systemctl enable nginx
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2456345.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!