nginx简易搭建及反向代理

一、安装nginx

1.下载nginx

下载地址:http://nginx.org/download/nginx-1.6.2.tar.gz

[root@localhost src]# pwd
/usr/local/src
[root@localhost src]# ls
nginx-1.6.2.tar.gz

2.解压nginx

[root@localhost src]# tar -zxvf nginx-1.6.2.tar.gz

3.编译安装

[root@localhost src]# cd nginx-1.6.2
[root@localhost nginx-1.6.2]# ./configure
[root@localhost nginx-1.6.2]# make
[root@localhost nginx-1.6.2]# make install

问题:

./configure: error: C compiler cc is not found
./configure: error: the HTTP rewrite module requires the PCRE library.
./configure: error: the HTTP gzip module requires the zlib library.

解决办法:下载依赖包
[root@localhost nginx-1.6.2]# yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel

4.启动nginx

[root@localhost nginx-1.6.2]# whereis nginx
nginx: /usr/local/nginx
[root@localhost nginx-1.6.2]# cd /usr/local/nginx/
[root@localhost nginx]# /usr/local/nginx/sbin/nginx 
[root@localhost nginx]# ps -aux | grep nginx
root      16291  0.0  0.0  20376   588 ?        Ss   17:36   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    16292  0.0  0.0  20800  1004 ?        S    17:36   0:00 nginx: worker process
root      16294  0.0  0.0 112828   972 pts/0    S+   17:36   0:00 grep --color=auto nginx

5.浏览器访问

在这里插入图片描述

注意:页面出不来可关闭防火墙试试

[root@localhost nginx]# systemctl stop firewalld
[root@localhost nginx]# systemctl disable firewalld

二、nginx的反向代理

1.编辑配置文件

[root@localhost nginx]# vi /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  nginx.test.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
}
    server {
        listen       81;
        server_name  nginx.test1.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
}
    server {
        listen       82;
        server_name  nginx.test2.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

配置完文件要重启服务:
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s reload

2.配置域名重定向

编辑windows:C:\Windows\System32\drivers\etc下的hosts文件
加入这句话:
192.168.200.100 nginx.test.com nginx.test1.com nginx.test2.com

打开windows的cmd,ping一下域名是否正确指向了这个IP里:

C:\Users\ZhangLing>ping nginx.test.com

正在 Ping nginx.test.com [192.168.200.100] 具有 32 字节的数据:
来自 192.168.200.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.200.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.200.100 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.200.100 的回复: 字节=32 时间<1ms TTL=64

192.168.200.100 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

浏览器访问:
在这里插入图片描述


版权声明:本文为zhang_ZERO原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。