概述

由于自己的之前学习 nginx 只会简单使用,然后每次配置 nginx 都要找文档去了解怎么配置,有点麻烦,所以这里记录下一些常用的nginx 配置和配置的例子,到时候直接 copy 修改即可。基本上这些都是工作之后都会需要用到的配置,我在工作中用到挺多次的,但是基本我每次都会忘记的,Nginx 对应的配置应该怎么写。

nginx 的主要功能为 静态文件的服务器、负载均衡、重写或重定向url、正向代理、反向代理、隐藏文件的实际后缀 等。

这里使用的 nginx 版本为 1.16.0

配置文件的主要结构为:

nginx.conf

http{

  # 这个用于负载均衡的配置
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server backup2.example.com:8080 backup;
}
# 服务配置1,我们一般只要修改这里的信息即可
server{
listen 8080;
location / {
proxy_pass http://localhost:8080;
}
} # 这个 server 是服务配置2
server{
listen 80;
location ~ \.(gif|jpg|png)$ {
proxy_pass http://backend;
}
}
# includ 用来引入 nginx 在其他目录的配置文件,一般正式的公司项目会这样使用,并以每个域名或 server 块分成每个配置文件。
include cust_conf/mainconf; # 加多一个确定的文件,这样找不到文件的时候会报错,以免配置错目录
includ /data/host/config/*.conf;
}

启动相关

window 启动

cmd命令进入安装文件;

1、启动:C:\server\nginx-1.0.2>start nginxC:\server\nginx-1.0.2>nginx.exe

注:建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作。

参考:https://www.jianshu.com/p/01f3626cf25d

nginx 命令行参数

不像许多其他软件系统,Nginx 仅有几个命令行参数,完全通过配置文件来配置

-c </path/to/config> 为 Nginx 指定一个配置文件,来代替缺省的。

-t 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。用法:nginx -t, 这个命令也可以查看nginx文件的所在位置。

-v 显示 nginx 的版本。

-V 显示 nginx 的版本,编译器版本和配置参数。

启动、停止 和重新加载配置

通过执行 nginx 的可执行文件可以直接启动 nginx。只要 nginx 启动了,则可以通过执行 nginx 加 -s 参数来控制 nginx 的一些行为。用法如下:

nginx -s ${signal}

${signal} 可以为以下四个值:

  • stop — fast shutdown 快速停止
  • quit — graceful shutdown 优雅停止
  • reload — reloading the configuration file 重新加载配置文件
  • reopen — reopening the log files 重新打开日志文件

例如:可以通过执行 nginx -s reload 来重新加载配置文件,使更改过的配置文件生效。

静态文件的服务器

  前端文件或者图片服务的部署,一般会使用到这个功能,通过 nginx 服务器来分发(sering out)文件,然后用户从网络上能够通过 ip 或 域名直接访问到

1. 实现访问 stats-server.kanlon.com 实现将 /data/fr 下的目录作为静态文件访问

server {
# 访问端口81,并且访问的域名为 stats-server.kanlon.com 使用该配置
listen 81;
server_name stats-server.kanlon.com;
charset utf8;
# 设置自动查找index文件为false,避免安全性问题
autoindex off;
# 设置展示首页的文件
index index.html index.htm index.php;
# 如果是/img/ 路径,则转发到专门存放图片的路径/data/img/中
location /img/ {
alias /data/img/;
}
location / {
root /data/fr;
}
}

如果访问出现错误,可以看一下 logs/error.log 的错误日志,这里会打印出实际访问的文件路径。还有测试 nginx 的时候一定要注意是否启动了多个 nginx 否则可能更改了配置会以为自己设置错误不生效的(沉重教训)

负载均衡

通过 nginx 可以实现将请求自动转发都自己的指定的服务域名上,减少单台服务请求量和实现服务的高可用。

nginx 上默认是通过 ngx_http_upstream_module 模块实现

1. 配置访问 load-balancing-test.kanlon.com:83 则负载均衡到指定三个域名上

负载的三个域名地址:load-balancing-test-1.kanlon.com:84,load-balancing-test-2.kanlon.com:85,load-balancing-test-3.kanlon.com:86

其中 要求 load-balancing-test-3.kanlon.com:86 作为备份服务,load-balancing-test.kanlon.com-1:84 和load-balancing-test-2.kanlon.com:85 的请求数分布比例为 1:2

# 配置健康检查,当为502,503,504,404的时候表示服务不可用,60秒内有两个这样的失败请求则负载到另一个服务上
proxy_next_upstream http_502 http_503 http_504 http_404 error timeout invalid_header;
upstream balancing {
server load-balancing-test-1.kanlon.com:84 max_fails=1 fail_timeout=60s weight=1;
server load-balancing-test-2.kanlon.com:85 max_fails=2 fail_timeout=60s weight=2;
server load-balancing-test-3.kanlon.com:86 backup;
}
server {
# 访问端口83,并且访问的域名为 load-balancing-test.kanlon.com 使用该配置
listen 83;
server_name load-balancing-test.kanlon.com;
# 设置自动查找index文件为false,避免安全性问题
autoindex off;
# 设置展示首页的文件
index index.html index.htm index.php;
location / {
proxy_pass http://balancing;
}
} server {
# 访问端口84,并且访问的域名为 load-balancing-test-1.kanlon.com 使用该配置
listen 84;
server_name load-balancing-test-1.kanlon.com;
charset utf8;
# 设置自动查找index文件为false,避免安全性问题
autoindex off;
# 设置展示首页的文件
index index.html index.htm index.php;
location / {
# 通过在目录后面加上没有文件的目录可以模拟服务不可用
root /data/nginx/load-balancing/load-balancing-test-1;
}
}
server {
# 访问端口85,并且访问的域名为 load-balancing-test-2.kanlon.com 使用该配置
listen 85;
server_name load-balancing-test-2.kanlon.com;
charset utf8;
# 设置自动查找index文件为false,避免安全性问题
autoindex off;
# 设置展示首页的文件
index index.html index.htm index.php;
location / {
# 通过在目录后面加上没有文件的目录可以模拟服务不可用
root /data/nginx/load-balancing/load-balancing-test-2;
}
}
server {
# 访问端口86,并且访问的域名为 load-balancing-test-3.kanlon.com 使用该配置
listen 86;
server_name load-balancing-test-3.kanlon.com;
charset utf8;
# 设置自动查找index文件为false,避免安全性问题
autoindex off;
# 设置展示首页的文件
index index.html index.htm index.php;
location / {
root /data/nginx/load-balancing/load-balancing-test-3;
}
}

其中的包含了一些 nginx 的健康检查的指令

max_fails=number 设定Nginx与服务器通信的尝试失败的次数。在fail_timeout参数定义的时间段内,如果失败的次数达到此值,Nginx就认为服务器不可用。在下一个fail_timeout时间段,服务器不会再被尝试。 失败的尝试次数默认是1。设为0就会停止统计尝试次数,认为服务器是一直可用的。 你可以通过指令proxy_next_upstream、fastcgi_next_upstream和 memcached_next_upstream来配置什么是失败的尝试。 默认配置时,http_404状态不被认为是失败的尝试。

fail_timeout=time 设定服务器被认为不可用的时间段以及统计失败尝试次数的时间段。在这段时间中,服务器失败次数达到指定的尝试次数,服务器就被认为不可用。

重写或重定向 url

nginx 可以将匹配的 url 重定向到另外的 url 去,包含改变地址

指定地址重定向

rewrite-local.kanlon.com/rewrite/same/site/** 重定向到 rewrite-local.kanlon.com/rewrite2/same/site/** 和 将 rewrite-local.kanlon.com/rewrite/other/site 重定向到 rewrite-other.kanlon.com/rewrite2/same/site

server {
# 访问端口88,并且访问的域名为 rewrite-local.kanlon.com 使用该配置
listen 88;
server_name rewrite-local.kanlon.com;
charset utf8;
# 设置自动查找index文件为false,避免安全性问题
autoindex off;
# 设置展示首页的文件
index index.html index.htm index.php; # 转发到本域名的其它路径
location /rewrite/same/site {
rewrite ^/rewrite/same/site(.*)$ /rewrite2/same/site$1 last;
} # 转发到另外的域名,如果要 重定向,; 前面加上 permanent (永久重定向 301 即可),默认为 302 临时重定向
location /rewrite/other/site {
rewrite ^/rewrite/other/site(.*)$ http://rewrite-other.kanlon.com:89/rewrite2/same/site$1 permanent;
} location /rewrite2/same/site {
default_type text/html;
return 200 "$request_uri";
} location / {
root /data/nginx/rewrite-test/rewriter-local;
}
} server {
# 访问端口89,并且访问的域名为 rewrite-other.kanlon.com 使用该配置
listen 89;
server_name rewrite-other.kanlon.com;
charset utf8;
# 设置自动查找index文件为false,避免安全性问题
autoindex off;
# 设置展示首页的文件
index index.html index.htm index.php; location /rewrite2/same/site {
default_type text/html;
return 200 "$request_uri";
}
location / {
root /data/nginx/rewrite-test/rewriter-other;
}
}

正向代理

正向代理,就是好像我们平常使用的 vpn 那样,我们的访问的域名不会变,只不过通过 nginx 来帮我们发送请求和接收请求,并返回自己客户端。nginx 默认支持 http 协议的正向代理,如果要支持 https 需要安装组件。

nginx 配置如下:

server {
listen 90;
server_name forward-agent.kanlon.com;
# 这里配置 DNS 服务器的IP地址
resolver 8.8.8.8;
location / {
proxy_pass http://$http_host$request_uri;
}
}

然后,再在自己电脑上配置代理服务器和端口,即可完成正向代理。

反向代理

反向代理,则直接访问 nginx 的域名来替代访问自己原来的要访问资源的域名,然后得到原资源的域名返回的结果

server {
listen 91;
server_name reverse-proxy.kanlon.com; #设置代理
location / {
proxy_pass http://127.0.0.1:8080;
}
}

隐藏文件的实际后缀

有时候我们想让访问静态html文件的时候,需要隐藏掉后缀.html,可以像下面这样配置

server {
listen 81;
charset utf8;
autoindex off;
index index.html index.htm index.php;
try_files $uri $uri/ /index.html?$query_string;
# 如果是以html或者 htm 结尾,则直接返回对应文件,以免引起nginx循环查找
location ~ \.(htm|html)$ {
root /data/nginx_static;
}
location / {
# 如果访问的文件不存在,则通过访问时添加后缀来隐藏URL中的后缀
if (!-e $request_filename){
rewrite ^(.*)$ /$1.html last;
break;
}
root /data/nginx_static;
} }

这样的话,假设项目根目录下有login.html 文件,原只能通过访问 127.0.0.1/login.html 访问到,设置之后,通过 127.0.0.1/login 或者 127.0.0.1/login.html 都能访问到

参考文档

  1. nginx 官方文档
  2. nginx 中文文档
  3. Nginx官网翻译
  4. Windows下Nginx的启动、停止、重启等命令
  5. Nginx正向代理与反向代理
  6. nginx 常见正则匹配符号表示
  7. Nginx实战系列之功能篇----后端节点健康检查
  8. nginx 可视化界面配置
  9. 让nginx上的静态网页在访问的时候没有html后缀

工作必会的Nginx的启动安装和常用配置例子的更多相关文章

  1. nginx基本运维及常用配置

    nginx基本运维及常用配置 ========================================================== 基本运维 nginx 的启动 nginx -c /p ...

  2. nginx安装以及常用配置

    nginx的源码安装 0 安装相关软件:yum -y install pcre-devel zlib-devel openssl-devel 1 下载 nginx-1.14.0.tar.gz 2 安装 ...

  3. Nginx(一):安装与常用命令

    简介   Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,特点是占有内存少,并发能 力强,事实上nginx的并发能力确实在同类型的网页服务器中表现 ...

  4. 【Nginx】之安装使用和配置SSL支持

    本文采用的是nginx源码安装 1.下载nginx源码包 wget http://nginx.org/download/nginx-1.8.0.tar 或者登录nginx官网下载更高版本 2.ngin ...

  5. Nginx 简介与安装、常用的命令和配置文件

    1.nginx 简介(1)介绍 nginx 的应用场景和具体可以做什么事情 (2)介绍什么是反向代理 (3)介绍什么是负载均衡 (4)介绍什么是动静分离 2.nginx 安装(1)介绍 nginx 在 ...

  6. Linux工具安装和常用配置

    1 常用开发工具安装 1 安装Mysql ①基本安装 wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm: s ...

  7. Redis安装及常用配置

    Redis安装说明 大多数企业都是基于Linux服务器来部署项目,而且Redis官方也没有提供Windows版本的安装包.因此课程中我们会基于Linux系统来安装Redis. 此处选择的Linux版本 ...

  8. tomcat安装以及常用配置

    目录 一 什么是tomcat 二 tomcat 的版本: 三 tomcat的下载 3.1 tomcat9版本下载链接 3.2 tomcat8.5版本下载链接 四 tomcat的安装 4.1 java环 ...

  9. Docker安装和常用配置【Linux】

    Linux下安装配置docker 安装指南:https://developer.aliyun.com/article/110806 一.配置国内镜像源 1.1 设置国内阿里巴巴下载源 [root@lo ...

  10. 【linux】之安装mysql常用配置

    下载mysql地址 http://dev.mysql.com/downloads/mysql/ 选择下面这个 查看是否存在mysql安装包 rpm -qa|grep -i mysql 删除mysql安 ...

随机推荐

  1. Qt编写地图综合应用54-动态点位标注

    一.前言 动态点位标注是定制的一个功能模块,提供直接地图上选点设置标记点,点位信息用结构体存储,其中包括了经度.纬度.速度.时间等信息,单击对应的标注点可以显示详细的弹框信息,弹框信息采用自定义的ht ...

  2. Qt编写安防视频监控系统29-掉线重连

    一.前言 掉线重连在很早很早以前就做了,基本上的方法都是搞个变量存储最后收到图片的时间,然后开个定时器判断,如果不在暂停模式下,当前时间和最后收到图片的时间差值超过了设定的超时时间,比如5s则认为掉线 ...

  3. Centos7安装VNCserver,并设置为开机自启动服务的方法

    参考链接: 1.How To Install and Configure VNC Remote Access for the GNOME Desktop on CentOS 7 2.Centos7作为 ...

  4. WebClient 用法小结

    进来的项目中要实现能够在windows service中调用指定项目的链接页面.由于访问页面时候使用的是ie浏览器或其他浏览器,所以想起用webclient类. 如果只想从特定的URI请求文件,则使用 ...

  5. ArchLinux安装后常见问题的解决

    Q:UEFI引导grub-install报错:variables are not support on this system A:进入安装u盘的时候是传统引导模式,传统模式下无法安装UEFI启动,解 ...

  6. 优化永不止步:TinyVue v3.20.0 正式发布,更美观的官网UI,更友好的文档搜索,更强大的主题配置能力~

    你好,我是 Kagol,个人公众号:前端开源星球. 我们非常高兴地宣布,2024年12月4日,TinyVue 发布了 v3.20.0 . 本次 3.20.0 版本主要有以下重大变更: OpenTiny ...

  7. 免费-高清免费视频录像软件OBS

    OBS studio 是免费开源的. https://obsproject.com/download 中文绿色版: http://www.xitongzhijia.net/soft/151705.ht ...

  8. SpringCloud(2)---入门篇

    SpringCloud(6)---熔断降级理解.Hystrix实战 一.概念 1.为什么需要熔断降级 (1)需求背景 它是系统负载过高,突发流量或者网络等各种异常情况介绍,常用的解决方案. 在一个分布 ...

  9. MySQL如果数据存在则更新,不存在则插入

    如果数据存在则更新,不存在则插入,MySQL有duplicate.replace into.replace三种方式如何更新数据? insert ignore into 又是如何插入数据的呢? 准备表和 ...

  10. Vim编辑器退出的多种方法

    当文本编辑结束之后,通常需要退出编辑器.退出编辑器又分为4种情况:保存退出.正常退出.不保存退出及强制退出.下面简单说下吧!   1.先介绍一下保存退出.当我们编辑或修改好了文件内容,如图.   我们 ...