快速配置!nginx两种代理配置:映射目录,映射端口

1.打开nginx安装目录下conf文件中的nginx.conf

加上:

include vhost/*.conf;
这个是增加配置文件,之后的两种配置文件都放在vhost文件夹中(简单来说就是让nginx识别vhost中的conf文件)
如下:
在这里插入图片描述

2.映射目录

在vhost文件夹中创建image.conf文件,复制下面代码

server {
    listen 80;
    autoindex off;
    server_name 127.0.0.1;
    access_log c:/access.log combined;
    index index.html index.htm index.jsp index.php; #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
     return 404;
      }
    location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* {
    deny all;
     }
    location / {
     root D:\ftpfile\img;
     add_header Access-Control-Allow-Origin *;
      }
 }

在这里插入图片描述
这个意思就是将127.0.0.1:80映射为我主机
文件夹(hhh里面有我一张自拍…)

测试有无错误:cmd走起:
在这里插入图片描述
无错误。接着重启nginx
在这里插入图片描述
验证:浏览器打开:
在这里插入图片描述
访问到我的一张自拍了。

3.映射端口

server {
    listen 80;
    autoindex on;
    server_name 127.0.0.1;
    access_log c:/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ) {
        return 404;
    }
    location / {
        proxy_pass http://127.0.0.1:8080;
        add_header Access-Control-Allow-Origin *;
  }
}

这个等于是把我主机的80端口号(nginx)映射到了我主机的8080端口号(tomcat)
所以我在浏览器输入:127.0.0.1 访问到的就是tomcat首页了

最后

在这里插入图片描述
如果是代理文件夹。开了on会显示所有文件。
如果是off则会出现以下页面:看不到所有文件,但是却可以访问。这样可以不让对方看到你的所有文件
在这里插入图片描述

发布了34 篇原创文章 · 获赞 0 · 访问量 599

猜你喜欢

转载自blog.csdn.net/weixin_44841849/article/details/104736356