当前位置: 首页 > news >正文

Nginx常用配置

Windows版本Nginx开机自启动

可直接下载已经配置好的文件,点击即可下载:Windows版本Nginx1.26.0

下载WinSW v2.12.0

首先从https://github.com/winsw/winsw/releases下载WinSW v2.12.0
在这里插入图片描述

下载Nginx

下载地址https://nginx.org/en/download.html
在这里插入图片描述

修改配置

1、将下载的WinSW-x64.exe拷贝到nginx的解压目录,我这里是D:\Server\nginx\nginx-1.26.0
2、将WinSW-x64.exe修改为nginx-service.exe
3、新增一个nginx-service.xml文件,内容如下

<service><id>Nginx</id><name>Nginx Service</name><description>Nginx服务</description><logpath>D:\Server\nginx\nginx-1.26.0\logs\</logpath><logmode>roll</logmode><log mode="roll-by-size">     <sizeThreshold>10240</sizeThreshold>     <keepFiles>8</keepFiles>   </log><executable>D:\Server\nginx\nginx-1.26.0\nginx.exe</executable><stopexecutable>D:\Server\nginx\nginx-1.26.0\nginx.exe -s stop</stopexecutable>
</service>

命令

用管理员身份打开命令行工具,安装服务执行nginx-service.exe install,卸载服务执行nginx-service.exe uninstall

D:\Server\nginx\nginx-1.26.0>
D:\Server\nginx\nginx-1.26.0>nginx-service.exe install
2024-09-07 21:59:18,214 INFO  - Installing service 'Nginx Service (Nginx)'...
2024-09-07 21:59:18,248 INFO  - Service 'Nginx Service (Nginx)' was installed successfully.D:\Server\nginx\nginx-1.26.0>
D:\Server\nginx\nginx-1.26.0>nginx-service.exe uninstall
2024-09-07 21:59:28,039 INFO  - Uninstalling service 'Nginx Service (Nginx)'...
2024-09-07 21:59:28,044 INFO  - Service 'Nginx Service (Nginx)' was uninstalled successfully.

安装成功后,在服务管理器中可以看到
在这里插入图片描述

禁止其它域名访问

当一个有多个域名同时配置到一台主机时,这时使用这些域名都可以访问网站,如果要限制其他域名访问,可使用如下配置:

http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;client_max_body_size 200M;# 端口禁止其它二级域名访问server {listen      443 ssl default_server;listen      8443 ssl default_server;server_name _;ssl_certificate cert/test.com.pem;ssl_certificate_key cert/test.com.key;server_tokens off;error_page  403              /403.html;location = /403.html {root   html;}error_page  404              /404.html;location = /404.html {root   html;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}return 403;}

在NGINX的配置文件中,server_name _; 是一个特殊的指令,用于匹配任何未明确指定的域名。当一个HTTP请求到达NGINX服务器时,它会根据请求头中的Host字段来确定应该由哪个server块来处理这个请求。
当使用server_name _;时,这个server块将作为默认服务器来处理那些没有明确匹配到其他server_name的请求。这在你有多个域名或子域名,并且希望为没有匹配到的请求提供一个默认处理方式时非常有用。例如:

http {server {listen 80;server_name example.com;location / {# 处理example.com的请求}}server {listen 80;server_name _;location / {# 处理未匹配到的域名请求}}
}

在这个例子中,如果一个请求的Host头是example.com,那么第一个server块将会处理这个请求。如果Host头是其他任何域名,那么第二个server块(使用server_name _;)将会作为默认服务器来处理这个请求。

禁止其它二级域名访问

当有多个二级域名都指向一个主机,可以采用如下方式进行限制,注意这里server_name *.test.com;

server {listen       443 ssl;server_name abc.test.com;ssl_certificate cert/test.com.pem;ssl_certificate_key cert/test.com.key;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;# ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;ssl_ciphers ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384;ssl_protocols TLSv1.2 TLSv1.3;ssl_prefer_server_ciphers on;server_tokens off;keepalive_timeout  65;client_max_body_size 200M;proxy_hide_header Access-Control-Allow-Origin;proxy_hide_header Access-Control-Allow-Credentials;proxy_hide_header Access-Control-Allow-Methods;proxy_hide_header Access-Control-Allow-Headers;proxy_hide_header Vary;add_header Content-Security-Policy "script-src 'self';" always;add_header 'Access-Control-Allow-Origin' 'https://192.168.31.2:443/';add_header 'Access-Control-Allow-Origin' 'https://abc.test.com:5443/';add_header 'Access-Control-Allow-Credentials' 'true';add_header 'Access-Control-Allow-Methods' 'GET,POST';add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';if ($request_method ~* OPTIONS) {return 403;}# 限制Swagger访问location ~ ^/api/v2 {return 403;}location / {root   html;index  index.html index.htm;}error_page  403              /403.html;location = /403.html {root   html;}error_page  404              /404.html;location = /404.html {root   html;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}# 443端口禁止其它二级域名访问server {listen      443 ssl;server_name *.test.com;ssl_certificate cert/test.com.pem;ssl_certificate_key cert/test.com.key;server_tokens off;error_page  403              /403.html;location = /403.html {root   html;}error_page  404              /404.html;location = /404.html {root   html;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}return 500;}

禁止Nginx显示版本

使用server_tokens off,可参看上面的示例配置。

http://www.lryc.cn/news/432567.html

相关文章:

  • 前端开发中遇到的小问题以及解决方案记录2
  • Qt-常用控件(3)-输入类
  • 使用Docker启动Redis容器并映射端口
  • 用fastapi搭建cpca地址提取服务接口
  • libvncclient编写多线程qt的VNC客户端
  • 视频处理基础之gradio框架实现
  • 黑马点评2——商户查询缓存(P37店铺类型查询业务添加缓存练习题答案)redis缓存、更新、穿透、雪崩、击穿、工具封装
  • 概率DP (由一道绿题引起的若干问题。目前为一些老题,蒟蒻的尝试学习1.0)
  • [Python]生成器和yield关键字
  • Nginx 负载均衡+高可用 集群部署(Keepalived+LVS DR模式)
  • 算法 | 基础 | 出现奇数次的数字
  • log4j 控制台和文件输出乱码问题解决
  • 在国产芯片上实现YOLOv5/v8图像AI识别-【4.2】RK3588获取USB摄像头图像推流RTSP更多内容见视频
  • TCP/IP协议栈详解及其在现代网络中的应用
  • 亚信安全荣获“2024年网络安全优秀创新成果大赛”优胜奖
  • 如何从硬盘恢复已删除/丢失的文件?硬盘恢复已删除的文件技巧
  • [Linux]:权限
  • 启动Spring Boot报错
  • 部署project_exam_system项目——及容器的编排
  • 网络工程师学习笔记——无线通信网
  • Vue(十三) 路由、路由嵌套、query、param传参、propos、replace属性。编程式路由导航,特有的生命周期函数,路由守卫
  • ArgoUML与StarUML的安装
  • 828华为云征文|华为云服务器Flexus X搭建悟空crm管理系统——助力企业云上管理(解决APP Referer校验失败问题)
  • 计算机毕业设计选题推荐-健康健身追踪系统-运动健身系统-Java/Python项目实战
  • FPGA开发:初识FPGA × 开发环境
  • 电脑驱动分类
  • 理解C++全局对象析构顺序与 IPC 资源管理:避免 coredump
  • 云计算之大数据(下)
  • 硬件工程师笔试面试知识器件篇——二极管
  • 操作系统安全保护