Nginx反向代理服务器安装与配置

​一、Nginx简介

1)Nginx是什么?

  • Nginx是一款轻量级Web服务器、也是一款反向代理服务器

2)Nginx能干什么?

  • Nginx能干的事情很多,这里简要罗列一些:

(1)可直接支持Rails和PHP的程序

(2)可作为HTTP反向代理服务器

(3)作为负载均衡服务器

(4)作为邮件代理服务器

(5)帮助实现前端动静分离

3)有什么特点?

(1)高稳定

(2)高性能

(3)资源占用少

(4)功能丰富

(5)模块化结构

(6)支持热部署


二、Nginx安装

       Linux:安装环境、安装步骤、安装命令、验证

安装系统环境:CentOS 6.8 64位

安装版本:1.10.2

安装步骤:

  1. 安装gcc(命令:yum install gcc)

    备注:可以输入gcc -v查询版本信息,看系统是否自带安装

  2. 安装pcre(命令:yum install pcre-devel)

  3. 安装zlib(命令:yum install zlib zlib-devel)

  4. 安装operssl(命令:yum install openssl openssl-devel)

  5. 下载源码包,选择稳定版本,解压缩安装(http://www.nginx.org/)

    也可以使用命令下载:wget http://nginx.org/download/nginx-1.10.2.tar.gz或访问:http://learning.happymmall.com/nginx/

  6. 下载完成后,解压:tar -zxvf nginx-1.10.2.tar.gz

  7. Nginx安装:进入nginx目录之后执行 ./configure

1)也可以指定安装目录,增加参数 --prefix=/usr/nginx

2)如果不指定路径,可以通过whereis nginx进行查询

3)默认安装在/usr/local/nginx

继续执行:make-->make install


备注:如需支持ssl,才需安装openssl

综合命令:yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel


    Windows:安装环境、安装步骤、安装命令、验证

安装系统环境:Windows7 64位

安装版本:1.10.2

安装步骤:

  1. 下载

  2. 解压缩

  3. 运行nginx.exe,通过双击图标或者cmd命令行运行


三、Nginx常用命令

  • 测试配置文件

    安装路径下的/nginx/sbin/nginx -t

  • 启动命令

    安装路径下的/nginx/sbin/nginx

  • 停止命令

    安装路径下的/nginx/sbin/nginx -s stop

    或者:nginx -s quit

  • 重启命令

    安装路径下的/nginx/sbin/nginx -s reload

  • 查看进程命令

    ps -ef | grep nginx

  • 平滑重启

    kill -HUP(Nginx主进程号,即查看进程命令查到的PID)

  • 增加防火墙访问权限

    1)sudo vim /etc/sysconfig/iptables

    2)-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

    3)保存退出

    4)重启防火墙:sudo service iptables restart


四、Nginx项目配置及验证(虚拟域名)

  • 配置步骤

    1)编辑 sudo vim /usr/local/nginx/conf/nginx.conf

    增加 include vhost/*.conf

    保存退出

    备注:http://learning.happymmall.com/nginx/linux_conf


2)在 /usr/local/nginx/conf/目录新建vhost文件夹。即:/usr/local/nginx/conf/vhost

3)创建域名转发配置文件

http://www.liugy.xin


liugy.com.conf

img.liugy.com.conf

s.liugy.con.conf


4)启动(重启)验证

启动:${nginx}/sbin/nginx

重启:${nginx}/sbin/nginx -s reload

注:${nginx}代表安装在系统中的路径,例如 /usr/local/nginx


5)访问验证

使用默认80端口访问验证:http://localhost:80 或 http://127.0.0.1:80


6)既指向端口又指向目录

server {

listen 80;

autoindex on;

server_name happymmall.com www.happymmall.com;

access_log /usr/local/nginx/logs/access.log combined;

index index.html index.htm index.jsp index.php;

if ( $query_string ~* ".*[\;'\<\>].*" ){

        return 404;

        }


location = / {

        root /product/front/mmall_fe/dist/view;

        index index.html;

}


location ~ .*\.html$ {

        root /product/front/mmall_fe/dist/view;

        index index.html;

}


location / {

        proxy_pass http://127.0.0.1:8080/;

        }


location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {

        proxy_pass http://127.0.0.1:8080;

        expires 30d;

        }


location ~ .*\.(js|css)?$ {

        proxy_pass http://127.0.0.1:8080;

        expires 7d;

        }

}


执行顺序:

server是大节点,

listen 80:监听80端口

autoindex on:是否创建首页的索引目录(值有:on 、off)

server_name:二级域名,当nginx接到二级域名请求过来后,转到 location节点处  proxy_pass  处的配置地址


7)指向目录配置方法


8)测试验证


五、Nginx本地玩耍注意事项

1)可以配置域名转发,但是请一定要配置host,并且使用host生效之后才可以,设置完成之后要重启浏览器

1、Linux:

(1)sudo vim /etc/hosts

(2)添加好对应的域名及ip

(3):wq保存退出


2、Windows:

(1)进入 C:\Windows\System32\drivers\etc

(2)用记事本打开hosts文件

(3)添加好对应的域名及ip

(4)保存退出

例如:

#CentOS

192.168.10.110  www.liugy.com

192.168.10.110  img.liugy.com

192.168.10.110  s.liugy.com


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值