以下内容来自 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. 3万字长文概述:通俗易懂告诉你什么是.NET?什么是.NET Framework?什么是.NET Core?

    [转载]通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core? 什么是.NET?什么是.NET Framework?本文将从上往下,循序渐进的介绍一系列相关.NET的 ...

  2. Markdown数学公式语法

    详细网址:Markdown数学公式语法

  3. SpringBoot2.0 整合 SpringSecurity 框架,实现用户权限安全管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.Security简介 1.基础概念 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方 ...

  4. Java题库——Chapter8 对象和类

    1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______ ...

  5. PAT 1008 Elevator 数学

    The highest building in our city has only one elevator. A request list is made up with N positive nu ...

  6. Jupyter Notebook 使用小记

    简介 Jupyter Notebook 是一款几乎综合所有编程语言,能够把软件代码.计算输出.解释文档.多媒体资源整合在一起的多功能科学计算平台.具有如下优点: 整合所有资源 交互性编程体验 零成本重 ...

  7. 前端深入之js篇丨Array数组操作从入门到成神Up Up Up,持续更新中

    写在前面 随着前端深入的不断学习,发现数组这个数据结构在前端中有着相当大的存在感,由于我初学前端的时候并没有系统性的学习数组,所以我将通过这篇文章同你一起学习数组,希望我们能一起进步,学会熟练操作数组 ...

  8. 查看Oracle被锁的表以及如何解锁

    注意权限问题 1.查看是否有被锁的表: select b.owner,b.object_name,a.session_id,a.locked_modefrom v$locked_object a,db ...

  9. JPA的基本注解

    场景 JPA入门简介与搭建HelloWorld(附代码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103473937 ...

  10. web渗透测试

    信息收集 网络搜索 目录遍历:site:域名 intitle:index.of 配置文件泄露:site:域名 ext:xml | ext:conf | ext:cnf | ext:reg | ext: ...