nginx常用命令

sudo nginx // 开启nginx服务器
sudo nginx -s reload // 重启nginx服务器
sudo nginx -s stop // 关闭nginx
nginx -t // 检查nginx配置,如果出现以下提示表示配置成功
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful  

nginx代理配置

1、proxy_pass

在nginx中配置proxy_pass时:

如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;

如果没有/,则会把匹配的路径部分也给代理走。

 location ^~ /support/
{
 proxy_cache js_cache;
 proxy_set_header Host www.xudengwei.com;
 proxy_pass http://www.xudengwei.com/;
}

如上面的配置,如果请求的url是http://servername/support/test.html
会被代理成http://www.xudengwei.com/test.html

而如果这么配置

location ^~ /support/
{
 proxy_cache js_cache;
 proxy_set_header Host www.xudengwei.com;
 proxy_pass http://www.xudengwei.com;
}

则会被代理到http://www.xudengwei.com/support/test.htm

2、rewrite

2.1 我们可以用如下的rewrite来实现上述/的功能

// 匹配任何以/support/开头的请求
location ^~ /support/
{
 proxy_cache js_cache;
 proxy_set_header Host www.xudengwei.com;
 rewrite /support/(.+)//1 break;
 proxy_pass http://www.xudengwei.com;
}  

2.2 rewrite中的$1/$2/$3...

server {
listen 80;
server_name dev.xudengwei.com;
location / {:
// 输入dev.xudengwei.com/test1/baidu 会重定向到 www.baidu.com,这里的$1就是上一个正则匹配的结果值
rewrite /test1/(.*) www.$1.com break;
}
}

3、last和break、permanent

last:last 和 break一样 它们都会终止此 location 中其他它rewrite模块指令的执行,但是 last 立即发起新一轮的 location 匹配 而 break 则不会

permanent: 永久性重定向。请求日志中的状态码为301

4、location

server {
server_name website.com;
location = /abcd {
......
}
}
测试:
http://website.com/abcd # 正好完全匹配
http://website.com/ABCD # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
http://website.com/abcd?param1m2 # 忽略查询串参数,这里就是 /abcd 后面的 ?param1m2
http://website.com/abcd/ # 不匹配,因为末尾存在反斜杠
http://website.com/abcde # 不匹配,因为不是完全匹配

项目cases

// 以下的nginx方向代理配置用于将浏览器请求代理到本地服务器

server {
listen 80;
server_name dev.xudengwei.com;
location / {
  # 相对路径是dll,assets开头的,都会被代理到proxy_pass定义的host,如果是全路径都走local,rewrite就不用写了,直接proxy_pass
rewrite '^(/(dll/|assets/).*)$' $1 break;
proxy_pass http://127.0.0.1:3000;
}
location ^~ /leo/ {
# dev
proxy_set_header Host dev.xudengwei.com;
proxy_pass http://39.106.39.185;
}
}

nginx常用命令汇总的更多相关文章

  1. Docker笔记:常用命令汇总

    Docker常用命令汇总 启动服务 [root@localhost ~]# service docker start Redirecting to /bin/systemctl start docke ...

  2. 20145222《信息安全系统设计基础》Linux常用命令汇总

    学习Linux时常用命令汇总 通过Ctrl+f键可在该网页搜索到你想要的命令. Linux中命令格式为:command [options] [arguments] //中括号代表是可选的,即有些命令不 ...

  3. Oozie命令行常用命令汇总[转]

    Oozie命令行常用命令汇总 有时候脚本跑多了就不愿意在OozieWeb端去看脚本的运行情况了.还好Oozie提供了很多命令行命令.能通过命令行直接检索自己想看到的脚本信息.在这里简单进行一下总结.一 ...

  4. vim常用命令汇总

    vim常用命令汇总: http://www.cnblogs.com/softwaretesting/archive/2011/07/12/2104435.html 定位 本行第一个字符 ctrl+$ ...

  5. 【Linux】nginx常用命令

    相关内容链接 Centos之安装Nginx及注意事项 [nginx]详细配置说明 nginx常用命令 [重新加载配置]sudo nginx -s reload [打开nginx配置]sudo vim ...

  6. Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡

    转自https://www.cnblogs.com/leeSmall/p/9351343.html 仅供个人学习 一.什么是正向代理.什么是反向代理 1. 正向代理,意思是一个位于客户端和原始服务器( ...

  7. nginx常用命令及简单配置

    nginx常用命令 nginx -c /usr/local/nginx/conf/nginx.conf 启动nginx(windows下start nginx); nginx -s quit 停止ng ...

  8. 【Linux】Linux 常用命令汇总

    查看软件xxx安装内容:dpkg -L xxx 查找软件库中的软件:apt-cache search 正则表达式 查找软件库中的软件:aptitude search 软件包 查找文件属于哪个包:dpk ...

  9. [svc][op]vim常用命令汇总

    vim常用命令汇总: 定位 本行第一个字符 ctrl+$ 本行最后一个字符 0gg 文章首行 ctrl+G 文章行尾 u 撤销(Undo) 删除 D 从当前位置删除到行尾 ("d$" ...

随机推荐

  1. Git—使用方法

    1.:插件的安装(eclipse LUNA版本之后已经自动集成,不需要安装插件). * 先打开该网页提供了对应版本的EGit,自己选择相应的版本.(http://wiki.eclipse.org/EG ...

  2. 错误处理:WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping

    今天在配置用户权限管理的时候,遇到了这么个错误: WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping.请添加一个名为 ...

  3. linux常用命令:ping 命令

    Linux系统的ping 命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说“ping一下某机器,看是不是开着”.不能打开网页时会说“你先ping网关地 址192.168.1.1试试 ...

  4. 20165207 Exp3 免杀原理与实践

    Exp3 免杀原理与实践 1.实验内容 1.1.使用msf 1.1.1. 确定基准线 首先看kali的ip 直接msfvenom的结果,不加其他的东西: 使用VirusTotal得到的检测这个程序得到 ...

  5. 阿里云运维部署工具AppDeploy详细教程

    AppDeploy是一个通过SSH实现的命令行工具,可完成应用部署和远程运维管理.当前工具实现为两个版本:普通版(伪代码描述语言)和Python版.Python版使用Python语法规则,可实现您的各 ...

  6. 使用Astah画UML类图经验总结

    从学习需求工程与UML开始,就开始接触到Astah这款软件,但是当时完全是为了对UML各种图的了解加深才使用了这款软件.当时画图,都是完全凭借自己想,并没有考虑实际情况,而且画的图都是很简单的,甚至有 ...

  7. 【运维技术】JENKINS管道部署容器化初探

    目标服务器安装docker参考官方文档 https://docs.docker.com/install/linux/docker-ce/centos/ (可选)在目标服务器上安装docker私服 ht ...

  8. SNMP学习笔记之SNMP树形结构介绍

    Basic command of SNMP: GET: The GET operation is a request sent by the manager to the managed device ...

  9. C++ VS2013环境编译使用sqlite数据库全过程

    转载:http://www.cnblogs.com/imoon/archive/2012/11/30/2796726.html 转载:https://blog.csdn.net/hjm4702192/ ...

  10. C# string字节数组转换

    string转byte[]:byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转string:string ...