Nginx反向代理配置、动静分离配置及应用

前期准备:为便于测试域名访问,此处在虚拟机搭建DNS服务器。

DNS主从服务器搭建参考:

《DNS主从服务器配置实战解析及案例:https://blog.csdn.net/Field_Yang/article/details/78596944

关闭防火墙

[root@testnginx]# service iptables stop

启动DNS服务器

[root@testnginx]# service named start

在宿主机配置虚拟机dns服务器为DNS服务器地址

192.168.88.2为虚拟机网关

安装Nginx

[root@test ~]#rpm -ivh nginx-1.8.1-1.el6.ngx.x86_64.rpm

[root@test ~]#rpm -ql nginx

/etc/logrotate.d/nginx

/etc/nginx

/etc/nginx/conf.d

/etc/nginx/conf.d/default.conf

/etc/nginx/conf.d/example_ssl.conf

/etc/nginx/fastcgi_params

/etc/nginx/koi-utf

/etc/nginx/koi-win

/etc/nginx/mime.types

/etc/nginx/nginx.conf

/etc/nginx/scgi_params

/etc/nginx/uwsgi_params

/etc/nginx/win-utf

/etc/rc.d/init.d/nginx

/etc/sysconfig/nginx

/usr/sbin/nginx

/usr/share/nginx

/usr/share/nginx/html

/usr/share/nginx/html/50x.html

/usr/share/nginx/html/index.html

/var/cache/nginx

/var/log/nginx

[root@test ~]# cd/etc/nginx

[root@testnginx]# ls

conf.d  fastcgi_params  koi-utf koi-win  mime.types  nginx.conf scgi_params  uwsgi_params  win-utf

[root@testnginx]# cd conf.d/

[[email protected]]# ls

bak.default.conf.20180418  default.conf example_ssl.conf

[[email protected]]# cd ..

编辑配置文件nginx.conf

[root@testnginx]# vi nginx.conf

user  nginx;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    log_format main  '$remote_addr - $remote_user[$time_local] "$request" '

                      '$status $body_bytes_sent"$http_referer" '

                     '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip on;

   include /etc/nginx/conf.d/*.conf;

}

案例1、根目录反向代理到本机的80端口

编辑配置文件default.conf,添加location /

[[email protected]]# vi  default.conf

server {

    listen       8080;

   server_name  127.0.0.1;

    #charset koi8-r;

    #access_log /var/log/nginx/log/host.access.log main; 

    location / {

       proxy_passhttp://127.0.0.1:80;

        indexindex.php index.html index.htm;

    }

    #error_page 404              /404.html;

    # redirect server error pages to the staticpage /50x.html

    error_page  500 502 503 504  /50x.html;

    location = /50x.html {

        root  /usr/share/nginx/html;

    }

    # proxy the PHP scripts to Apache listeningon 127.0.0.1:80

    #location ~ \.php$ {

    #   proxy_pass   http://127.0.0.1;

    #}

    # pass the PHP scripts to FastCGI serverlistening on 127.0.0.1:9000

    #location ~ \.php$ {

    #   root           html;

    #   fastcgi_pass   127.0.0.1:9000;

    #   fastcgi_index  index.php;

    #   fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

    #   include        fastcgi_params;

    #}

    # deny access to .htaccess files, ifApache's document root

    # concurs with nginx's one

    #location ~ /\.ht {

    #   deny  all;

    #}

}

[[email protected]]#  cd /var/www/html/

[root@testhtml]# ll

总用量 12

drwxr-xr-x. 2root root 4096 11月 19 17:03 bbs

drwxr-xr-x. 2root root 4096 11月 19 17:27 images

-rw-r--r--  1 root root 111 4月  18 20:40 index.html

编辑测试页面index.html


[root@test bbs]#cd ../images

[root@testimages]# ls

1.png  2.png 3.png  4.png  5.png

[root@testimages]#

启动80端口的httpd 和8080端口的Nginx

[root@www html]#service httpd start

正在启动 httpd:[确定]

[[email protected]]# service nginx start

正在启动 nginx:[确定]

使用curl解析测试访问:

[[email protected]]# curl http://192.168.88.130/bbs/

<h1>bbs ontest.field.com</h1>

[[email protected]]# curl http://192.168.88.130:8080/bbs/

<h1>bbs ontest.field.com</h1>

[[email protected]]# curl http://192.168.88.130:80

<h1> 测试Nginx反向代理到本机80端口 </h1>

<p>body:httpdon test.field.com</p>

<p>测试成功!</p>

[[email protected]]# curl http://192.168.88.130:8080

<h1> 测试Nginx反向代理到本机80端口 </h1>

<p>body:httpdon test.field.com</p>

<p>测试成功!</p>

访问80端口的httpd


访问8080端口的nginx ,测试是否能反向代理到本机80端口


访问8080端口的根目录下的bbs/,测试是否能反向代理到本机80端口的bbs/


案例2、Nginx下的/field/反向代理到http的/field/

测试新建location/field/

[[email protected]]# vi default.conf

server {

    listen      8080;

    server_name 127.0.0.1;

    #charset koi8-r;

    #access_log /var/log/nginx/log/host.access.log main; 

    location / {

       proxy_pass http://127.0.0.1:80;

        index index.php index.html index.htm;

    }

        location /field/ {

     proxy_pass http://192.168.88.130/field/;

    }

    error_page  500 502 503 504  /50x.html;

    location = /50x.html {

        root  /usr/share/nginx/html;

    }

}


使用curl解析页面

[root@www html]#curl http://test.field.com:80/field/

<h1> 测试访问/field目录 </h1>

<p> 新建 location /field/ </p>

<p> fielddir on test.field.com </p>

<p> 测试成功!</p> 

访问8080端口下的/field/,测试是否能放心代理到本机80端口的/field/

 http://test.field.com:8080/field/



案例3、反向代理之动静分离

 [root@test conf.d]# vi default.conf

server {

    listen      8080;

    server_name 127.0.0.1;

    #charset koi8-r;

    #access_log /var/log/nginx/log/host.access.log main; 

    location / {

       proxy_pass http://127.0.0.1:80;

        index index.php index.html index.htm;

}

        location /field/ {

     proxy_pass http://192.168.88.130/field/;

    }

    location ~* \.(avi|gif|mp3|jsp|do)$ {

    #proxy_pass http://web1.field.com:80;

        proxy_pass http://192.168.88.131:80;

        }

    error_page  500 502 503 504  /50x.html;

    location = /50x.html {

        root  /usr/share/nginx/html;

    }

}

访问192.168.88.131下的gif 、MP3、avi 文件



访问192.168.88.130:8080下的gif 、MP3、avi 文件,测试是否能反向代理至131服务器




案例4、使用域名做反向代理

[[email protected]]# vi default.conf

 server {

    listen      8080;

    server_name 127.0.0.1;

    #charset koi8-r;

    #access_log /var/log/nginx/log/host.access.log main;

    location / {

       proxy_pass http://127.0.0.1:80;

        index index.php index.html index.htm;

    }

    location ~* \.(avi|gif|mp3|jsp|do)$ {

    proxy_pass http://web1.field.com:80;

        #proxy_pass http://192.168.88.131:80;

        }

    #error_page 404              /404.html;

    # redirect server error pages to the staticpage /50x.html

    error_page  500 502 503 504  /50x.html;

    location = /50x.html {

        root  /usr/share/nginx/html;

    }

    # proxy the PHP scripts to Apache listeningon 127.0.0.1:80

    #location ~ \.php$ {

    #   proxy_pass   http://127.0.0.1;

    #}

    # pass the PHP scripts to FastCGI serverlistening on 127.0.0.1:9000

    #location ~ \.php$ {

    #   root           html;

    #   fastcgi_pass   127.0.0.1:9000;

    #   fastcgi_index  index.php;

    #   fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

    #   include        fastcgi_params;

    #}

    # deny access to .htaccess files, ifApache's document root

    # concurs with nginx's one

   #location ~ /\.ht {

"default.conf"49L, 1234C written

 [root@test conf.d]# service nginx reload

重新载入 nginx:[确定]

编辑测试页面


使用curl解析192.168.88.131:80页面

[[email protected]]# curl http://192.168.88.131:80

<h1> 域名访问测试Nginx反向代理 </h1>

<p>body:httpdon web1.field.com</p>

<p> 测试成功!</p>

<p><em>感谢关注nginx!!</em></p>

[[email protected]]#




猜你喜欢

转载自blog.csdn.net/field_yang/article/details/79999122