抛开SSL先不讲,针对使用WordPress搭建个人博客,Nginx的配置文件需要修改的地方不多。
不修改其实网站在运行时也不会有大问题,但总归有这有那的奇奇怪怪的小问题。
因此总结一下Nginx的配置文件的一些修改。
参考网站:
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; } }