以下内容来自 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. netty源码解析(4.0)-21 ByteBuf的设计原理

        io.netty.buffer包中是netty ByteBuf的实现.ByteBuf是一个二进制缓冲区的抽象接口,它的功能有: 可以随机访问.顺序访问. 支持基本数据类型(byte, shor ...

  2. 缓存AJAX的请求

      在客户端缓存Ajax请求 浏览器可以缓存图片.js文件.css文件,同样浏览器也可以缓存XML Http调用(当然这需要XML Http以get方式发送调用),这种缓存基于URL,当我们发送一个请 ...

  3. Eclipse 的快捷键

    1. 代码折叠的快捷键,默认是: Ctrl+Shift+Numpad_Divede(小键盘的/号) Ctrl+Shift+Numpad_Multiply(小键盘的*号) 2.删除一行:Ctrl+D 3 ...

  4. ORACLE DATAGUARD 日志传输状态监控

    ORACLE DATAGUARD的主备库同步,主要是依靠日志传输到备库,备库应用日志或归档来实现.当主.备库间日志传输出现GAP,备库将不再与主库同步.因此需对日志传输状态进行监控,确保主.备库间日志 ...

  5. 解决Python3.6.5+Django2.0集成xadmin后台点击添加或者内容详情报 list index out of range 的错误

    一 问题说明在创建Model的时候,如果存在类型是DateTimeField的字段,则在xadmin后端管理界面里,对该Model进行添加操作的时候,会报list index out of range ...

  6. How To Restore Rman Backups On A Different Node When The Directory Structures Are Different (Doc ID 419137.1)

    How To Restore Rman Backups On A Different Node When The Directory Structures Are Different (Doc ID ...

  7. bootrom/spl/uboot/linux逐级加载是如何实现的?

    关键词:bootrom.spl.uboot.linux.mksheader.sb_header.mkimage.image_header_t等等. 首先看一个典型的bootrom->spl-&g ...

  8. Octave绘图数据

    t = [0:0.01:0.98]   :设置一个步长为0.01的矩阵 y1 = sin(2*pi*4*t) :设置一个sin函数 plot(t,y1) :绘制出以 t 为横轴  以  y1为纵轴的图 ...

  9. 【python3基础】python3 神坑笔记

    目录 os 篇 os.listdir(path) 运算符篇 is vs. == 实例 1:判断两个整数相等 实例 2:argparse 传参 实例 3:np.where 命令行参数篇 Referenc ...

  10. Redux使用

    思想 应用中所有的state都以一个对象树的形式储存在一个单一的store中.唯一能改变state的办法是触发action,一个描述发生什么的对象.为了描述action如何改变state树,需要编写r ...