---
title: "Nginx管理静态资源"
author: "uule"
date: "2017-11-26T12:23:46Z"
tags: []
categories: ["编程语言"]
slug: "nginx"
draft: false
lastmod: "2017-11-26T12:23:46Z"
id: 2400918
url: "https://uule.iteye.com/blog/2400918"
---

[配置Nginx前端 + Apache后端服务器环境](http://blog.csdn.net/cuipengchong/article/details/44242085)

说明：

nginx处理静态内容是把好手，apache虽然占用内存多了点，性能上稍逊，但一直比较稳健。倒是nginx的FastCGI有时候会出现502 Bad Gateway错误。一个可选的方法是nginx做前端代理，处理静态内容，动态请求统统转发给后端apache。

本文就是实现Nginx作为前端，apache作为后端。当用户访问的是80端口的nginx，然后nginx将静态内容留给自己，其余的转发给非80端口的apache，apache处理完毕后再回传给nginx。

 

nginx静态资源分离部署

[https://www.cnblogs.com/panxuejun/p/6027730.html](https://www.cnblogs.com/panxuejun/p/6027730.html)

[https://segmentfault.com/a/1190000010487262](https://segmentfault.com/a/1190000010487262)

```
server {
    listen       80;
    server_name  123.57.162.75;
    charset utf-8;
    index index.html index.htm index.jsp index.do;
    root /opt/nginx-1.4.7/html/resources;
    
    #配置Nginx动静分离，定义的静态页面直接从Nginx发布目录读取。
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ 
    { 
        root /opt/nginx-1.4.7/html/resources;
        #expires定义用户浏览器缓存的时间为7天，如果静态页面不常更新，可以设置更长，这样可以节省带宽和缓解服务器的压力
        expires      7d; 
    } 
}

server {
  server_name static.naice.me; // 你的域名或者 ip
  root /www/static-web/static-web; // 你的克隆到的项目路径
  index index.html; // 显示首页
  location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt){
    root /www/static-web/static-web;
    index  index.html index.htm;
  } // 静态文件访问
}
```

 

**[基于nginx的静态网页部署](https://www.2cto.com/kf/201706/648819.html)**

```
server {
        listen 8081;             # 监听本机所有 ip 上的 8081 端口
        server_name _;           # 域名：www.example.com 这里 "_" 代表获取匹配所有
        root /home/filename/;    # 站点根目录
 
        location / {             # 可有多个 location 用于配置路由地址
            try_files index.html =404;
        }
}
```

 

。。。