分享

参照nginx官方样本重新配置Wordpress站点

 louis2010 2019-03-10

nginx官网提供了常用blog/cms等系统的示例配置文件。Wordpress的在这里

基础的是这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
server {
        ## Your website name goes here.
        server_name domain.tld;
        ## Your only path reference.
        root /var/www/wordpress;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
        location / {
                # This is cool because no php is touched for static content.
                # include the '?$args' part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }
        location ~ \.php$ {
                #NOTE: You should have 'cgi.fix_pathinfo = 0;' in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

后面还有一些多站共存之类的配置。

按照这个范本我对我自己的配置做了一些调整,试了一下完全可行,比原来的好用。看来之前对nginx的配置还是不够了解。

我自己的配置大致是这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#禁止空主机头访问
server {
    listen       80 default_server;
    server_name _;
    return 500;
}
server {
    listen       80;
    server_name  youdomain.com www.yourdomain.com;
    #下面这条规定站点根目录,再往下的配置就以此为根
    root /usr/share/nginx/siterootpath;
    index index.php;
    #charset koi8-r;
    #下面这句是访问log,按需开关
    #access_log  /var/log/nginx/log/host.access.log  main;
    #favicon,站点的图标,可以显示在浏览器标题栏上,和robots.txt两个都不需要log
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location =/robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    # 所有静态文件都直通,解决了之前jpg等文件单独设置直通的问题,另外还可以直接支持固定链接
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    #自定义404
    #error_page  404              /404.html;
    #自定义错误页
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    #对上传目录禁止执行php,并阻止除图片以外的任何文件类型
    location /wp-content/uploads/ {
        if ($request_filename !~* \.(jpg|jpeg|gif|png)$) {
             return 403;
        }
        location ~ .*\.(php)?$ {
           deny all;
        }
    }
    # php文件发送到php5-fpm,现在这一段不需要写root目录了
    #
    location ~ \.php$ {
        #检查php文件是否真实存在,防止伪装成jpg的php等
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
    #js css 图片等文件最大化有效期,不记录访问
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多