美文网首页
nginx动静分离

nginx动静分离

作者: 一剑仙人跪_ | 来源:发表于2019-07-19 18:31 被阅读0次

    准备三台服务器 分别是代理, 静态, 动态

    yum装的nginx
    主配置文件:/etc/nginx/nginx.conf
    Nginx Server配置文件:/etc/nginx/conf.d/default.conf

    源码装的nginx配置文件都在: /etc/nginx/nginx.conf

    代理服务器配置

    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/access.log  main;
    
        location  ~ \.(php|jsp)$ {
         proxy_pass http://php;
         proxy_redirect default;
         proxy_set_header Host $http_host;
         proxy_set_header X-Real-IP $remote_addr;
         #proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
        }
    
        location ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$ {
                       proxy_pass http://static;
                       proxy_set_header Host $host:$server_port;
                      proxy_set_header X-Real-IP $remote_addr;
                     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }
    }
    
    
    [root@node1 conf.d]# cat upstrem.conf 
    upstream static {       
          server 192.168.205.153:80  weight=1 max_fails=1 fail_timeout=60s;
        } 
    upstream php {
          server 192.168.205.152:80  weight=1 max_fails=1 fail_timeout=60s; 
    } 
    

    静态服务器 配置文件

        server {
            listen     80;
            server_name  localhost;
    
            charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$ {
                root   /var/www/nginx;
            }
        }
    
    [root@node2 nginx]# ls /var/www/nginx/
    test1.png  test2.jpg
    

    测试:192.168.205.153/test1.png

    动态服务器 配置文件

    yum 安装php7.1 
    [root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm [root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm [root@nginx-server ~]#yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71wdevel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y 
    [root@nginx-server ~]#yum install -y php71w-fpm 
    [root@nginx-server ~]#systemctl start php-fpm 
    [root@nginx-server ~]#systemctl enable php-fpm
    
    server {
        listen       80;
        server_name  localhost;
    
        location ~ \.php$ {
           root          /home/nginx/html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
         }
    
    }
     
    
     [root@node3 ~]# cat /home/nginx/html/index.php 
    <?php
        phpinfo();
    ?>
    

    测试: 192.168.205.152/index.php

    最后测试用代理服务器访问

    相关文章

      网友评论

          本文标题:nginx动静分离

          本文链接:https://www.haomeiwen.com/subject/pooslctx.html