nginx动静分离

1、准备环境
准备一个nginx代理 两个http 分别处理动态和静态。

1.配置nginx反向代理upstream; 
upstream static {       
        server 10.0.105.196:80 weight=1 max_fails=1fail_timeout=60s;        
} 
upstream php {        
        server 10.0.105.200:80 weight=1 max_fails=1 fail_timeout=60s;       
 }     
      server {       
            listen      80;        
            server_name     localhost        
           #动态资源加载        
            location ~ \.(php|jsp)$ {            
                 proxy_pass http://phpserver;            
                 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;                
       }       
              #静态资源加载        
            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;               
             }       
 }

静态资源配置

server {         
          listen 80;        
          server_name     localhost;
 
           location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg) { 
                   root /home/www/nginx;       
         }
 }

动态资源配置:

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 
编辑nginx的配置文件: 
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; #站点根目录,取决 于root配置项            
                    include        fastcgi_params;  #包含nginx常量定义                
      }       
 }

你可能感兴趣的:(nginx动静分离)