首页 > 系统相关 >Nginx 常用配置

Nginx 常用配置

时间:2023-04-13 19:46:00浏览次数:34  
标签:index 常用 nginx 配置 share Nginx html 82 81

一、rewrite

在/usr/share/nginx/html/下创建abc.html文件

[root@localhost html]# cat /usr/share/nginx/html/81/abc.html 
abc

nginx配置如下

server {
    listen       81;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    location /test {
        root /usr/share/nginx/html;
        index  index.html index.htm;
        rewrite /test/(.*) /$1 permanent;
    }
    location /abc {
        root /usr/share/nginx/html/81;
        index  index.html index.htm;
    }

}

在浏览器访问http://192.168.10.66:81/test/abc.html,结果

 

 

 在浏览器访问http://192.168.10.66:81/test/abc/abc.html,结果

  •  结论:/abc.html和/abc是匹配的

二、proxy_pass

配置模拟81端口是代理,82端口是后端服务器

81端口配置如下

server {
    listen       81;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    location /test {
        root /usr/share/nginx/html;
        index  index.html index.htm;
        rewrite /test/(.*) /$1 permanent;
    }
    location /abc {
        root /usr/share/nginx/html/81;
        index  index.html index.htm;
    }
    location /p1 {
        proxy_pass http://127.0.0.1:82;
    }
}

82端口配置如下

server {
    listen       82;
    server_name  localhost;

    access_log  /var/log/nginx/82.access.log  main;

    location / {
        root   /usr/share/nginx/html/82;
        index  index.html index.htm;
    }
    location /p1 {
        root   /usr/share/nginx/;
        index  index.html index.htm;
    }
    location /p2 {
        root   /usr/share/nginx/html/82/p2;
        index  index.html index.htm;
    }
}

 

1、创建2个p.html

[root@localhost p1]# cat /usr/share/nginx/html/82/p1/p.html 
ppppp
[root@localhost p1]# cat /usr/share/nginx/p1/p.html nginx_p1

2、在浏览器访问192.168.10.66:81/p1/p.html,结果如下。根据日志看,访问的是192.168.10.66:82/p1/p.html

3、把81的proxy_pass http://127.0.0.1:82;改成proxy_pass http://127.0.0.1:82/;。

  • 创建文件p.html
[root@localhost 82]# cat /usr/share/nginx/html/82/p.html 
test_p

 

 

 

 4、把81的proxy_pass http://127.0.0.1:82;改成proxy_pass http://127.0.0.1:82/;后,。

  • 访问http://192.168.10.66:81/p1/p2/p.html,实际访问的是http://192.168.10.66:82//p2/p.html

 

 

  • 访问http://192.168.10.66:81/p1/p2/p3/p.html,实际访问的是http://192.168.10.66:82//p2/p3/p.html

 

  •  结论:把81的proxy_pass http://127.0.0.1:82;改成proxy_pass http://127.0.0.1:82/;,在后面加了/。不加/就是代理服务器用同一url访问后端,加了/后,代理服务器访问后端的url去掉/p1。

 

标签:index,常用,nginx,配置,share,Nginx,html,82,81
From: https://www.cnblogs.com/lfxx/p/17315553.html

相关文章

  • zabbix分布式监控服务 安装与配置
     zabbix安装与配置一、什么是zabbix及优缺点 Zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。 agent端:主机通过安装......
  • 【Docker】容器操作 mysql部署 redis部署 nginx部署 迁移与备份 Dockerfile
    目录上节回顾今日内容1容器操作2应用部署2.1mysql部署2.2redis2.3nginx3迁移与备份4Dockerfile练习上节回顾#1docker启动,停止,查看状态命令 systemctlstartdockersystemctlstopdockersystemctlstatusdockerdockerinfo#查看详情systemc......
  • 开源流媒体服务器ZLMediaKit在Windows上运行、配置、按需拉流拉取摄像头rtsp视频流)并
    场景目前市面上有很多开源的流媒体服务器解决方案,常见的有SRS、EasyDarwin、ZLMediaKit和Monibuca等。1、SRShttps://github.com/ossrs/srs2、EasyDarwinhttps://github.com/EasyDarwin/EasyDarwin3、Monibucahttps://github.com/Monibuca4、ZLMediaKithttps://github.co......
  • git常用命令汇总
    (文章目录)仓库#在当前目录新建一个Git代码库$gitinit#新建一个目录,将其初始化为Git代码库$gitinit[project-name]#下载一个项目和它的整个代码历史$gitclone[url]配置#显示当前的Git配置$gitconfig--list#编辑Git配置文件$gitconfig-e[--glob......
  • Spring IOC容器注解大全—基于Java的容器配置
    本节介绍了如何在你的Java代码中使用注解来配置Spring容器。它包括以下主题。基本概念:@Bean 和 @Configuration通过使用 AnnotationConfigApplicationContext 实例化Spring容器使用 @Bean 注解使用 @Configuration 注解构建基于Java的配置Bean定义配置PropertySource 抽象......
  • redis-4-2,使用docker在同一台服务器配置redis主从复制
    拉取redis镜像dockerpullredis下载redis配置文件https://redis.io/docs/management/config/在这里面找到需要的版本的配置文件使用配置文件的时候可能会出错,从docker拉取下来的最新镜像是6.2.8版本的......
  • mvc多拦截器配置
        ......
  • mvc拦截器配置与方法参数
          ......
  • Docker常用命令--镜像相关
    镜像相关镜像名称镜像名称一般分两部分组成:[repository]:[tag]。在没有指定tag时,默认是latest,代表最新版本的镜像  这里的mysql就是repository,5.7就是tag,合一起就是镜像名称,代表5.7版本的MySQL镜像。拉取、查看镜像从DockerHub中拉取一个nginx镜像并查看(1)首先去镜......
  • 分布式电源优化配置 二阶锥 考虑配电网二阶锥模型,运行主体包括光伏、微燃机以及负荷,创
    分布式电源优化配置二阶锥编程方法:采用matlab+yalmip编程,cplex或gurobi作为求解器。主要内容:考虑配电网二阶锥模型,运行主体包括光伏、微燃机以及负荷,创新性考虑敏感负荷及加权电压支撑能力指标,约束条件考虑潮流约束、电压电流约束、分布式电源容量约束、微燃机出力约束和光伏功......