博客
关于我
Nginx 常用配置清单
阅读量:799 次
发布时间:2023-02-15

本文共 1993 字,大约阅读时间需要 6 分钟。

Nginx 配置指南:常用配置示例

Nginx 是一款高性能的 HTTP 和反向代理 web 服务器,同时也支持 IMAP/POP3/SMTP 服务。它因其强大的功能、稳定性和低资源消耗而深受开发者喜爱。本文将总结一些常用的 Nginx 配置示例,希望能为您提供帮助。

侦听端口

server {    listen 80;   # 标准的 HTTP 端口    listen [::]:80;  # 在 IPv6 上侦听 80 端口    listen 443 ssl;  # 标准的 HTTPS 端口    listen [::]:443 ssl http2;  # 支持 HTTP/2 的 IPv6 端口    listen 80 ipv6only=on;  # 只在 IPv6 上侦听 80 端口}

访问日志

server {    access_log /path/to/access.log;}

域名

server {    server_name yourdomain.com;  #侦听 yourdomain.com    server_name yourdomain.com www.yourdomain.com;  #侦听多个域名    server_name *.yourdomain.com;  #侦听所有子域名    server_name yourdomain.*;  #侦听所有顶级域名    server_name "";  #侦听未指定主机名(仅限 IP 地址)}

静态资产

server {    listen 80;    server_name yourdomain.com;    location / {        root /path/to/website;    }}

重定向

server {    listen 80;    server_name www.yourdomain.com;    return 301 https://$host$request_uri;}server {    listen 80;    server_name www.yourdomain.com;    location /redirect-url {        return 301 https://otherdomain.com;    }}

反向代理

server {    listen 80;    server_name yourdomain.com;    location / {        proxy_pass http://0.0.0.0:3000;        # 0.0.0.0:3000 是您的应用服务器(如 Node.js)    }}

负载均衡

upstream node_js {    server 0.0.0.0:3000;    server 0.0.0.0:4000;    server 123.131.121.122;}server {    listen 80;    server_name yourdomain.com;    location / {        proxy_pass http://node_js;    }}

SSL 配置

server {    listen 443 ssl;    server_name yourdomain.com;    ssl on;    ssl_certificate /path/to/cert.pem;    ssl_certificate_key /path/to/privatekey.pem;    ssl_stapling on;    ssl_stapling_verify on;    ssl_trusted_certificate /path/to/fullchain.pem;    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    ssl_session_timeout 1h;    ssl_session_cache shared:SSL:50m;    add_header Strict-Transport-Security max-age=15768000;}server {    listen 80;    server_name yourdomain.com;    return 301 https://$host$request_uri;}

工具推荐

通过可视化工具可以更方便地配置 Nginx。推荐使用 GitHub 上的开源工具或在线编辑器,支持反向代理、HTTPS、HTTP/2、IPv6 等多种配置场景,帮助您快速生成 Nginx 配置文件。

转载地址:http://alcfk.baihongyu.com/

你可能感兴趣的文章
node防xss攻击插件
查看>>
noi 7827 质数的和与积
查看>>
NOIp2005 过河
查看>>
NOIP2014 提高组 Day2——寻找道路
查看>>
NOIp模拟赛二十九
查看>>
NOPI读取Excel
查看>>
NoSQL&MongoDB
查看>>
NoSQL介绍
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>