以下内容来自 SimulatedGREG/nginx-cheatsheet

通用设置

端口 listen

server {
# standard HTTP protocol
listen 80;

standard HTTPS protocol

listen 443 ssl;

listen on 80 using IPv6

listen [::]:80;

listen only on IPv6

listen [::]:80 ipv6only=on;

}

域名 server_name

server {
# Listen to yourdomain.com
server_name yourdomain.com;

Listen to multiple domains

server_name yourdomain.com www.yourdomain.com;

Listen to all sub-domains

server_name *.yourdomain.com;

Listen to all top-level domains

server_name yourdomain.*;

Listen to unspecified hostnames (listens to IP address itself)

server_name "";

}

访问日志 access_log

server {
# Relative or full path to log file
access_log /path/to/file.log;

Turn 'on' or 'off'

access_log on;

}

gzip, client_max_body_size

server {
# Turn gzip compression 'on' or 'off'
gzip on;

Limit client body size to 10mb

client_max_body_size 10M;

}

响应文件

静态文件

server {
listen 80;
server_name yourdomain.com; location / {

root /path/to/website;

}

}

支持前端路由

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

try_files $uri $uri/ /index.html;

}

}

重定向

301 永久重定向

用于比如 www.yourdomain.com 重定向到 yourdomain.com 或 将 http 重定向到 https

server {
listen 80;
server_name www.yourdomain.com;
return 301 http://yourdomain.com$request_uri;
}

302 临时重定向

server {
listen 80;
server_name yourdomain.com;
return 302 http://otherdomain.com;
}

重定向指定页面

server {
listen 80;
server_name yourdomain.com; location /redirect-url {

return 301 http://otherdomain.com;

}

}

反向代理

基本

server {
listen 80;
server_name yourdomain.com; location / {

proxy_pass http://0.0.0.0:3000;

# where 0.0.0.0:3000 is your Node.js Server bound on 0.0.0.0 listing on port 3000

}

}

进阶

upstream node_js {
server 0.0.0.0:3000;
# where 0.0.0.0:3000 is your Node.js Server bound on 0.0.0.0 listing on port 3000
} server {

listen 80;

server_name yourdomain.com; location / {

proxy_pass http://node_js;

}

}

socket 相关

upstream node_js {
server 0.0.0.0:3000;
} server {

listen 80;

server_name yourdomain.com; location / {

proxy_pass http://node_js;

proxy_redirect off;

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";

proxy_set_header Host $host;
# not required but useful for applications with heavy WebSocket usage
# as it increases the default timeout configuration of 60
proxy_read_timeout 80;

}

}

TLS/SSL (HTTPS)

以下仅是简单 HTTPS 示例,并不是可用于生产环境的最优配置。

相关资源:

server {
listen 443 ssl;
server_name yourdomain.com; ssl on; ssl_certificate /path/to/cert.pem;

ssl_certificate_key /path/to/privkey.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 1d;

ssl_session_cache shared:SSL:50m;

add_header Strict-Transport-Security max-age=15768000;

}

Permanent redirect for HTTP to HTTPS

server {

listen 80;

server_name yourdomain.com;

return 301 https://$host$request_uri;

}

大型应用

负载均衡

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;

}

}

NGINX 配置清单的更多相关文章

  1. 从一份配置清单详解 Nginx 服务器配置

      概述 在前面< Nginx 服务器开箱体验> 一文中我们从开箱到体验,感受了一下 Nginx 服务器的魅力.Nginx 是轻量级的高性能 Web 服务器,提供了诸如 HTTP 代理和反 ...

  2. puppet的配置清单书写

    puppet的配置清单书写 1使用数组,合并同类的 例如你想安装很多软件,如果分开来写的话,很麻烦,不简洁,这时我们可以使用数组来完成 以前我们这样来写 class packages{ package ...

  3. nginx配置反向代理或跳转出现400问题处理记录

    午休完上班后,同事说测试站点访问接口出现400 Bad Request  Request Header Or Cookie Too Large提示,心想还好是测试服务器出现问题,影响不大,不过也赶紧上 ...

  4. Windos环境用Nginx配置反向代理和负载均衡

    Windos环境用Nginx配置反向代理和负载均衡 引言:在前后端分离架构下,难免会遇到跨域问题.目前的解决方案大致有JSONP,反向代理,CORS这三种方式.JSONP兼容性良好,最大的缺点是只支持 ...

  5. Windows下Nginx配置SSL实现Https访问(包含证书生成)

    Vincent.李   Windows下Nginx配置SSL实现Https访问(包含证书生成) Windows下Nginx配置SSL实现Https访问(包含证书生成) 首先要说明为什么要实现https ...

  6. Nginx 配置简述

    不论是本地开发,还是远程到 Server 开发,还是给提供 demo 给人看效果,我们时常需要对 Nginx 做配置,Nginx 的配置项相当多,如果考虑性能配置起来会比较麻烦.不过,我们往往只是需要 ...

  7. Nginx配置详解

    序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...

  8. Nginx配置Https

    1.申请证书: https://console.qcloud.com/ssl?utm_source=yingyongbao&utm_medium=ssl&utm_campaign=qc ...

  9. nginx配置为windows服务中的坑

    网上搜索“nginx 配置为windows服务”,很容易搜索到使用windows server warpper来配置,于是按照网上的方法我从github上的链接下载了1.17版本,前面都很顺利,很容易 ...

随机推荐

  1. 《Google软件测试之道》

    Google软件测试之道 Google对质量的理解 质量不等于测试,即质量不是被测出来的 开发和测试应该并肩齐驱,测试就是开发过程中不可缺少的一部分 质量是一种预防行为而不是检测 Google对软件测 ...

  2. xamarin调试android部署到模拟器错误记录:Deployment failed Mono.AndroidTools.InstallFailedException: Unexpected install output: Error: Could not access the Package Manager. Is the system running?

    问题记录: 1.生成 ok. 2.昨天也是能部署到模拟器的. 但是今天部署的时候就报了这样的一个错误 Deployment failed Mono.AndroidTools.InstallFailed ...

  3. 1+x 证书 Web 前端开发中级理论考试(试卷 8 )含答案

    1+x 证书 Web 前端开发中级理论考试(试卷 8 ) 官方QQ群 转载请注明来源:妙笔生花个人博客http://blog.zh66.club/index.php/archives/438/ 一.单 ...

  4. 前端笔记之微信小程序(一)初识微信小程序&WXSS与CSS|WXML与HTML的差异&像素和DPR

    一.小程序概述 2017 年 1 月 9 日小程序正式上线,腾讯开放了个人开发者开发小程序,小程序从此就开始火爆,这一年,小程序狂揽 4 亿用户.1.7 亿的日常活跃,上线 58 万个.这是一个巨大的 ...

  5. Comet OJ - Contest #10 B题 沉鱼落雁

    ###题目链接### 题目大意:有 n 个正整数,每个正整数代表一个成语,正整数一样则成语相同.同一个正整数最多只会出现 3 次. 求一种排列,使得这个排列中,相同成语的间隔最小值最大,输出这个最小间 ...

  6. 禧云Redis跨机房双向同步实践

    编者荐语: 2019年4月16日跨机房Redis同步中间件(Rotter)上线,团餐率先商用: 以下文章来源于云纵达摩院 ,作者杨海波   禧云信息/研发中心/杨海波 20191115 关键词:Rot ...

  7. 利用Python进行数据分析-Pandas(第五部分-数据规整:聚合、合并和重塑)

    在许多应用中,数据可能分散在许多文件或数据库中,存储的形式也不利于分析.本部分关注可以聚合.合并.重塑数据的方法. 1.层次化索引 层次化索引(hierarchical indexing)是panda ...

  8. c#中的Nullable(可空类型)

    在C#中使用Nullable类型(给整型赋null值的方法) 在C#1.x的版本中,一个值类型变量是不可以被赋予null值的,否则会产生异常.在C#2.0中,微软提供了Nullable类型,允许用它定 ...

  9. springboot之jpa支持

    相关pom依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  10. 【SDUT】2019SDUTACM第一次选拔赛 F- X的追求道路

    Problem Description X在大家的帮助下终于找到了一个妹纸,于是开始了漫漫的追求之路,那么大家猜一猜X能不能追的上呢? X初始对妹纸有一个心动值,妹纸对X有一个好感值,在追求时发生的的 ...