您当前的位置: 首页 >  服务器

小志的博客

暂无认证

  • 1浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Nginx——nginx作为代理服务器(反向代理示例)

小志的博客 发布时间:2021-07-31 22:37:38 ,浏览量:1

目录
    • 一、代理服务原理
    • 二、nginx配置代理的语法
    • 三、nginx反向代理演示示例

一、代理服务原理
  • 参考lz此博文:https://wwwxz.blog.csdn.net/article/details/119277923
二、nginx配置代理的语法
  • Syntax:proxy_pass URL; URL表示所有客户端请求请求到nginx代理服务器后,nginx代理服务器向原始服务器请求的url。URL格式:http://ip+端口/路径
  • Default:—— 表示默认没有配置;
  • Context:location; 表示需要在location块中;
三、nginx反向代理演示示例

1、配置nginx访问html页面的路径

  • 创建nginxproxy.html页面,上传到/opt/app/html目录下

    
    	
    		
    		测试nginx反向代理
    	
    
    	
    		nginx反向代理!!!!
    	
    
    

2、编辑 nginx.conf 配置文件可以看到在/etc/nginx/conf.d/目录下可以创建子配置文件,如下图: 在这里插入图片描述 3、lz在/etc/nginx/conf.d/目录下创建一个origin_server.conf(原始服务配置文件)和proxy_server.conf(代理服务配置文件),内容分别如下:

  • origin_server.conf配置文件内容:(注:因为原始服务用的是8080端口,需要配置8080端口不被外界所访问)

    server {
            listen       8080; 
            server_name  localhost;
    	
    		location / {
    			root /opt/app/html;
    			index index.html index.htm;
    		}	
            error_page 404 /404.html;
            location = /404.html {
            }
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
            }
        }
    

    在这里插入图片描述

  • proxy_server.conf配置文件内容如下:

    server {
            listen       80; #端口
            server_name  localhost; #访问的ip
    	
    		location / {
    			root /opt/app/html;
    			index index.html index.htm;
    		}
    		#配置代理服务器访问原始服务器的地址
    		location ~ /nginxproxy.html$ {
    			proxy_pass http://localhost:8080;
    		}		
    			
            error_page 404 /404.html;
            location = /404.html {
            }
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
            }
        }
    

    在这里插入图片描述

4、启动nginx服务并检查配置文件

  • 启动nginx服务

    [root@localhost conf.d]# systemctl start nginx.service
    
  • 检查配置修改的配置文件是否正确,返回successful表示配置文件修改无错

    [root@localhost nginx]# nginx -t -c /etc/nginx/nginx.conf
    

    在这里插入图片描述

5、重新加载nginx配置文件,并查看

[root@localhost conf.d]# nginx -s reload -c /etc/nginx/nginx.conf

6、查看本机启用nginx的端口

[root@localhost conf.d]# netstat -luntp|grep nginx

在这里插入图片描述 7、在浏览器输入http://localhost/nginxproxy.html地址(即通过代理服务的80端口访问,可以访问到页面),如下图:

在这里插入图片描述 8、在浏览器输入http://localhost:8080/nginxproxy.html地址(即直接访问原始服务的8080端口访问,不可以访问到页面),如下图:

在这里插入图片描述

关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.2330s