• fastcgi_param  PHP_VALUE "upload_max_filesize=100M \n post_max_size=100M";

    添加在 location ~ .php$ 块中

    location ~ \.php$ {
            #fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param  PHP_VALUE "upload_max_filesize=100M \n post_max_size=100M";
            include        fastcgi_params;
    }

  • pip3 install nginxfmt
    nginxfmt /path/conf

    slomkowski/nginx-config-formatter


  • 我的服务器是NGINX,所以先查看NGINX的错误日志

    tail /var/log/nginx/error.log -n 10

    上传一次大图像,然后看日志最后一条的信息是

    2021/04/23 10:17:11 [error] 212420#212420: *166782 client intended to send too large body: 6849556 bytes, client: 120.85.112.147, server: zuoridangnian.com, request: “POST /wp-admin/async-upload.php HTTP/1.1”, host: “zuoridangnian.com”, referrer: “https://zuoridangnian.com/wp-admin/upload.php”

    错误是:

    client intended to send too large body

    解决办法是:

    在NGINX配置文件中添加

    client_max_body_size 80m;

    添加在 server 内对单独网站生效,添加在 http 块中对所有网站生效

    如果你的服务器错误日志中是其它错误,就查其它的解决办法


  • 首先停用apache

    systemctl stop apache2

    安装nginx和php-fpm

    apt install nginx php-fpm

    创建nginx配置文件 nano /etc/nginx/sites-available/zrdn.conf

    server {
        listen 80;
        server_name zuoridangnian.com;
        root /var/www/zrdn;
    
        index index.html index.htm index.php;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
         }
    
        location ~ /\.ht {
            deny all;
        }
    
    }
    

    启用站点

    ln -s /etc/nginx/sites-available/zrdn.conf /etc/nginx/sites-enabled

    重启nginx

    systemctl restart nginx

    配置ssl证书,安装certbot nginx

    apt-get install certbot python3-certbot-nginx

    配置证书

    certbot –nginx

    选择域名,然后选择第二项,renew 更新证书,然后因为我用了cloudflare的cdn,所以选择No Redirect

    如果你也用了cloudflare 并且ssl是flexible模式,则你vps其实可以不用安装ssl证书

    重载一下nginx,大功告成

    systemctl restart nginx


  • 编辑 /etc/yum.repos.d/nginx.repo 文件,加入如下内容

    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=0
    enabled=1

    然后 yum update && yum install nginx 即可


  • Nginx内建了一个状态页,可以用来查看nginx当前的状态及运行情况。
    在你的 nginx.conf 配置的server中添加如下配置:

    location /nginx_status {
    stub_status on;
    access_log off;
    allow all;
    deny all;
    }
    

    location字段为显示状态的路径。
    allow字段为设置的可以允许访问的IP地址,你可以设为all即所有IP均可访问,或者你的IP,或者本地IP。
    然后重启 nginx,即可成功。

    sudo systemctl restart nginx
    

    然后访问你设置的路径,你就可以看到类似下面的信息

    Active connections: 2
    server accepts handled requests
     2 2 5
    Reading: 0 Writing: 1 Waiting: 1
    

    说明:

    • Active connection: 活跃的连接数量
    • server accepts handled requests: 总共处理了2个连接,成功创建了2次握手,总共处理了5个请求
    • Reading: 读取客户端的连接数
    • Writing: 响应数据到客户端的数量
    • Waitting: 开启keep-alive的情况下,这个只等于active-(Reading+Writing),意思就是nginx已经处理完正在等候下一次请求指令的驻留连接

  • 可能是因为我所使用的VPS所分配的IP之前被别人使用过的原因,我发现有一个域名被解析到了我的IP上,而且这个域名的年龄还不算低,在谷歌上的权重比我本站还高,我一看,这还了得,吓的立马关闭了VPS。
    但是我记得我又在Nginx上设置了所有解析到这个IP上的域名都会301跳转到我这个网站,虽然点开那个域名也会跳转到我这个网站,但是我感觉这样还是不行,最好让那些解析到我IP上的域名彻底无法访问才行。
    编辑nginx.conf文件,在http下加入

    server {
        listen 80 default;
        server_name _;
        return 444;
    }
    

    444是nginx返回的表示无响应的状态码,即不会返回任何信息,当然你也可以改成其它不会返回你网站信息的状态码,如403、404、500等等都可以。


  • 仅以此贴记录一下我在配置Nginx HTTPS时遇到的坑

    所有问题的报错信息均来自systemctl status nginx

    unknown directive “ssl”

    这个问题是由于你在nginx.conf中配置ssl on所致

    原因:你没有给nginx安装https ssl模块

    解决办法:重新安装一次你的当前版本

    举例:我在上一篇文章中直接使用./configure生成的配置信息,你需要回到你的nginx源码解压目录,再次运行,并加上https模块

    cd nginx-1.13.8
    ./configure --with-http_ssl_module
    make
    cp objs/nginx /usr/local/nginx/sbin/nginx
    /usr/local/nginx/sbin/nginx
    

    感谢Nginx如何安装https-ssl证书 – 简书

    checking for OpenSSL library … not found

    接着,如果你在运行./configure --with-http_ssl_module之后,得到报错信息

    checking for OpenSSL library ... not found
    ./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl= option.
    

    解决办法:安装libssl-dev包(如果你没安装openssl,你需要安装一下)

    感谢ubuntu – Can’t compile nginx with SSL support, OpenSSL not found – Server Fault

    [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)

    接着,如果你debug了半天你的nginx,然后重新启动,发现启动不了,查看status信息看到类似如下信息

    Feb 11 02:45:44 ubuntu-s-1vcpu-1gb-sfo2-01 nginx[9635]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
    Feb 11 02:45:45 ubuntu-s-1vcpu-1gb-sfo2-01 nginx[9635]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    Feb 11 02:45:45 ubuntu-s-1vcpu-1gb-sfo2-01 nginx[9635]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
    Feb 11 02:45:45 ubuntu-s-1vcpu-1gb-sfo2-01 nginx[9635]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    Feb 11 02:45:45 ubuntu-s-1vcpu-1gb-sfo2-01 nginx[9635]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
    

    原因:你的80/443端口被占用了

    解决办法:杀死占用进程

    fuser -k 80/tcp
    fuser -k 443/tcp
    

    感谢[emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use) – EasyEngine

    总结

    遇事多google!

    感谢google!


  • 为什么要编译安装?

    • 默认软件源中的包都不是最新的(Centos、Debian等都是如此)
    • 编译安装更加稳定
    • 自定义程度高

    安装方法

    编译安装十分简单,我之前在Ubuntu 16.04上面通过apt安装的nginx1.10版本,太老了,如果你之前也安装过,直接卸载即可。

    apt-get purge nginx
    rm -rf /etc/nginx
    

    接着安装:

    ## 目前最新的正式版是1.13.8,好像还有测试版到1.15了,这里安装正式版,当然还有稳定版是1.12
    wget http://nginx.org/download/nginx-1.13.8.tar.gz
    tar zxvf nginx-1.13.8.tar.gz
    cd nginx-1.13.8
    ## ./configure有很多自定义选项,可查看文档
    ./configure
    make
    make install
    ## 启动nginx,如果你安装到其它位置还需要加c参数指定配置文件
    /usr/local/nginx/sbin/nginx
    

    配置systemctl

    当然那样子启动不够方便,我们可以将nginx添加到systemd服务中去

    新建/lib/systemd/system/nginx.service文件,加入如下内容:

    [Unit]
    Description=nginx - high performance web server
    After=network.target remote-fs.target nss-lookup.target
    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
    [Install]
    WantedBy=multi-user.target
    

    其中以Exec开头的命令,如果你是安装到其它路径,需要更改

    接着就可以像使用包管理安装一样来愉快的使用systemctl了

    例如加入开机自启

    systemctl enable nginx
    

    重启服务

    systemctl restart nginx
    

    enjoy!