前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >12.16 Nginx代理

12.16 Nginx代理

作者头像
运维小白
发布2018-02-06 16:03:16
9460
发布2018-02-06 16:03:16
举报
文章被收录于专栏:运维小白

Nginx代理目录概要

输入图片说明
输入图片说明
  • cd /usr/local/nginx/conf/vhost
  • vim proxy.conf //加入如下内容
代码语言:javascript
复制
server
{
    listen 80;
    server_name ask.apelearn.com;

    location /
    {
        proxy_pass      http://121.201.9.155/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Nginx代理

输入图片说明
输入图片说明
  • 需求:
    • 用户需要访问web服务器,但用户因为各种原因没办法访问或者访问很慢(私网无访问、境内访问国外服务器),所以,就需要一个能访问web服务器的代理者,让用户通过代理服务器访问
  • 解决方法
    • 创建代理服务器
  1. 首先切换目录cd /usr/local/nginx/conf/vhost
代码语言:javascript
复制
[root@hanfeng ~]# cd /usr/local/nginx/conf/vhost
[root@hanfeng vhost]# 
  1. 新建一个配置文件vim proxy.conf
代码语言:javascript
复制
[root@hanfeng vhost]# vim proxy.conf

加入以下内容
server
{
    listen 80;
    server_name ask.apelearn.com;                       //定义域名,论坛的网站
    location /
    {
        proxy_pass      http://121.201.9.155/;         //定义域名,论坛的IP
        proxy_set_header Host   $host;                   //定义访问的域名 为 $host =server_name ask.apelearn.com
        proxy_set_header X-Real-IP      $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
保存退出
  1. 配置文件中,没有了root,因为这是一个代理服务器,它不需要访问本地服务器上的任何文件
  2. 在配置完成后,这台虚拟机就可以访问ask.apelearn.com论坛了
  3. 检查配置文件语法错误,并重新加载配置文件
代码语言:javascript
复制
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]# 
  1. robots是针对蜘蛛的索引的一个列表,一般网站都会有robots
代码语言:javascript
复制
[root@hanfeng vhost]# curl ask.apelearn.com/robots.txt
#
# robots.txt for MiWen
#

User-agent: *

Disallow: /?/admin/
Disallow: /?/people/
Disallow: /?/question/
Disallow: /account/
Disallow: /app/
Disallow: /cache/
Disallow: /install/
Disallow: /models/
Disallow: /crond/run/
Disallow: /search/
Disallow: /static/
Disallow: /setting/
Disallow: /system/
Disallow: /tmp/
Disallow: /themes/
Disallow: /uploads/
Disallow: /url-*
Disallow: /views/
Disallow: /*/ajax/[root@hanfeng vhost]# 
[root@hanfeng vhost]# 
  1. 测试代理是否成功,指定本机的IP,也能去访问
代码语言:javascript
复制
[root@hanfeng vhost]# curl -x127.0.0.1:80  ask.apelearn.com/robots.txt
#
# robots.txt for MiWen
#

User-agent: *

Disallow: /?/admin/
Disallow: /?/people/
Disallow: /?/question/
Disallow: /account/
Disallow: /app/
Disallow: /cache/
Disallow: /install/
Disallow: /models/
Disallow: /crond/run/
Disallow: /search/
Disallow: /static/
Disallow: /setting/
Disallow: /system/
Disallow: /tmp/
Disallow: /themes/
Disallow: /uploads/
Disallow: /url-*
Disallow: /views/
Disallow: /*/ajax/[root@hanfeng vhost]# 
  1. 正常情况下,不去配置这个代理,是不可能通过本地访问到远程的站点的
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Nginx代理目录概要
  • Nginx代理
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档