以下内容来自 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. 2019-2020-1 20199305《Linux内核原理与分析》第九周作业

    进程的切换和一般执行过程 (一)进程调度的时机 (1)关键问题 何为进程切换?就是进程调度时机到来时从就绪进程队列中能够挑选一个进程执行,占用CPU时间,那么就有两个关键问题:一是什么时间去挑选一个就 ...

  2. 多项式总结(STAGE 1)

    这么难的专题居然只给了这么短时间... 然而在NC的教导之下还是有一定的收获的. 必须打广告:0,1,2,3 附带一个垃圾博客:-1 按照习惯,堆砌结论而不加证明. Section1 导数: 基本形式 ...

  3. asp.net MVC通用权限管理系统-响应式布局-源码

    一.Angel工作室简单通用权限系统简介 AngelRM(Asp.net MVC Web api)是基于asp.net(C#)MVC+前端bootstrap+ztree+lodash+jquery技术 ...

  4. 微信小程序跳转传参参数丢失?

    垂死病中惊坐起,笑问 Bug 何处来?! 1.先是大写字母作祟 前两天发布了「柒留言」v2.0.0 新版本,结果...你懂的嘛,没有 Bug 的程序不是好程序,写不出 Bug 的程序员不是好程序员. ...

  5. jQuery从零开始(三)-----ajax

    jq当中的ajax技术 $.ajax $.get() $.post() $.load() $.ajax({ url:'请求文件的地址', type:'请求文件使用的方法', data:'向请求的api ...

  6. iOS 自定义导航栏

    参考链接:https://blog.csdn.net/huanglinxiao/article/details/100537988 demo下载地址:https://github.com/huangx ...

  7. iOS----------文字逐个显示

    参考文档: https://blog.csdn.net/et295394330/article/details/50529862

  8. springboot架构下运用shiro后在configuration,通过@Value获取不到值,总是为null

    通过网上查找资料,是因为shiro的bean @Beanpublic LifecycleBeanPostProcessor lifecycleBeanPostProcessor() { return ...

  9. IDEA org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    引用地址:https://guozh.net/idea-org-apache-ibatis-binding-bindingexception-invalid-bound-statement-not-f ...

  10. Oracle创建表、删除表、修改表(添加字段、修改字段、删除字段)语句总结

    创建表: create table 表名 ( 字段名1 字段类型 默认值 是否为空 , 字段名2 字段类型 默认值 是否为空, 字段名3 字段类型 默认值 是否为空, ...... ); 创建一个us ...