针对使用WordPress搭建个人博客的Nginx的配置文件的修改

抛开SSL先不讲,针对使用WordPress搭建个人博客,Nginx的配置文件需要修改的地方不多。

不修改其实网站在运行时也不会有大问题,但总归有这有那的奇奇怪怪的小问题。

因此总结一下Nginx的配置文件的一些修改。

参考网站:

WordPress work with Nginx

Full Example Configuration

LNMP + WordPress 搭建个人网站教程:Debian下最新版本LNMP环境搭建以及安装WordPress建造你自己的个人网站

 

允许上传超过1M的文件

Nginx默认上传文件大小为1M,因此在上传超过1M的文件时会提示出错。

需要在nginx.conf文件里添加以下语句,大小可以任意更改。

http {
    ......

    client_max_body_size 16m;

}

 

关闭favicon.ico不存在时的报错

如果不关闭这个的话,Nginx的error日志会定期的不断的抱怨没找到favicon.ico文件。

因此在conf.d/default.conf文件里添加以下语句。

Server {
    ......

    location = /favicon.ico { 
        log_not_found off; 
        access_log off; 
    }

}
 

设置网页路径配置规则

为了子网页的路径简洁美观、可读性高,我们通常会在WordPress的设置里的固定链接里设置自己喜欢的格式。 但是这里需要修改Nginx的配置规则,不然会出现404 not found。 所以在conf.d/default.conf文件里添加try_files语句,这样Nginx就可以根据规则查找相应网页啦。
server {
    ......

    location / {
        ......

        try_files $uri $uri/ /index.php?$args;

    }

}
 

配置rebots.txt文件,允许访问以及关闭报错

允许访问rebots.txt文件,以配合搜索引擎抓取网址内容。 同时关闭找不到rebots.txt文件时,Nginx的不断报错。 在conf.d/default.conf文件里添加以下语句。
server {
    ......

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

}
 

网站中图片有关的配置

主要是需要配置网站中图片缓存有效期,以及关闭图片找不到的报错。 在conf.d/default.conf文件里添加以下语句。
server {
    ......

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

}
 

禁止访问一些配置文件

一般来说是为了防止被黑,嘛,不过我们这种小站究竟有没有人想黑也是个问题。 在conf.d/default.conf文件里添加以下语句。
server {
    ......

    location ~ \.user\.ini$ {
        deny  all;
    }

}
 

添加一些FastCGI设置,优化网站

在conf.d/default.conf文件里添加以下语句。
Server {
    ......

    location ~ \.php$ {
        ......

        fastcgi_intercept_errors on;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;

    }

}
 

我的Nginx配置文件

贴出我的配置文件,因为配置了SSL因此跟我前面描述的有更多的内容。仅供参考。
nginx.conf
user  www-data www-data;
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;

    client_max_body_size 16m;

    keepalive_timeout  65;

    gzip  on;

    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  10m;

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

 

conf.d/default.conf
server {
    listen 80 default;
    server_name _;
    return 500;
}

 

conf.d/junjhome.conf
server {
    listen 80;
    server_name  junjhome.com www.junjhome.com;
    #add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
    return 301 https://$host$request_uri;
}

 

conf.d/junjhome_ssl.conf
server {
    listen       443 ssl;
    server_name  junjhome.com www.junjhome.com;

    root /home/wordpress;

    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
    #add_header X-Frame-Options DENY;
    #add_header X-Content-Type-Options nosniff;

    ssl         on;
    ssl_certificate /etc/letsencrypt/live/junjhome.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/junjhome.com/privkey.pem;
    ssl_protocols   TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers     HIGH:!aNULL:!MD5;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location / {
    #    root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$args;
    }

    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;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    #    root           html;
        fastcgi_pass   unix:/run/php/php7.2-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }

    location ~ \.user\.ini$ {
        deny  all;
    }
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值