标签: nginx

  • nginx https配置模板

    server {
        listen 443 ssl http2 default_server;
        server_name  www.lwbj.cn;
        root /var/www/www.lwbj.cn;
        index  index.html index.htm index.php;
    
        #文件上传大小限制  必须要放在server下的server_name下
        client_max_body_size 200m;
    
        # 因为是默认的 https 站点,所以有可能是从 IP 进来的请求,那么把它跳转到域名
        if ($host != 'www.lwbj.cn') {
            rewrite ^/(.*)$ https://lwbj.cn/$1 permanent;
            break;
        }
    
        ssl_certificate /etc/nginx/ssl/www.lwbj.cn.pem;
        ssl_certificate_key /etc/nginx/ssl/www.lwbj.cn.key;
    
        #加上TLSv1,HTTPS检测会报PCI DSS不合规
        ssl_protocols  TLSv1.2 TLSv1.3;# Requires nginx >= 1.13.0 else use TLSv1.2
        ssl_prefer_server_ciphers on;
        ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
        ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
        ssl_session_timeout 10m;
        ssl_session_cache shared:SSL:10m;
        ssl_session_tickets off; # Requires nginx >= 1.5.9
        ssl_stapling on; # Requires nginx >= 1.3.7
        ssl_stapling_verify on; # Requires nginx => 1.3.7
    
        add_header X-Frame-Options SAMEORIGIN;
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        # pass PHP scripts to FastCGI server
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        }
    }
    
  • Nginx PHP 配置模板

    server {
        listen 80;
        root /var/www/www.example.com;
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.php index.nginx-debian.html;
    
        server_name www.example.com;
    
        client_max_body_size 20m;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    
            # With php-fpm (or other unix sockets):
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        }
    }
  • nginx反代iis 支持泛域名,支持WordPress多站点

    nginx配置:

    upstream qd-aliyun-8006 {
        server x.x.x.x:8006;
    }
    
    server {
        listen 80;
        server_name softc.cc *.softc.cc;
    
        location / {
            proxy_pass http://qd-aliyun-8006;
            proxy_redirect default;
    
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-Port $remote_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Port  $server_port;
            proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr;
        }
    }
    

    注意是要有 proxy_set_header Host $host; 这一行。

  • laravel nginx配置

    编辑nginx的网站配置文件,在location / {} 里面添加如下代码:

    try_files $uri $uri/ /index.php?$query_string;
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=$1  last;
        break;
    }

  • Debian+Nginx+PHP HTTPS配置模板

    Nginx+PHP+HTTPS的配置模板,如果要部署其它网站,可以基于此进行修改:

    # 处理 HTTP 请求,所有请求都重定向到 https://lwbj.cn
    server {
        listen 80;
        server_name lwbj.cn www.lwbj.cn;  # 同时匹配带 www 和不带 www 的域名
    
        # 所有请求都重定向到 https://lwbj.cn
        return 301 https://lwbj.cn$request_uri;
    }
    
    # 处理不带 www 的 HTTPS 请求,重定向到带 www 的域名
    server {
        listen 443 ssl http2;
        server_name lwbj.cn;  # 不带 www 的域名
    
        ssl_certificate /etc/nginx/ssl/www.lwbj.cn.pem;  # 证书路径
        ssl_certificate_key /etc/nginx/ssl/www.lwbj.cn.key;  # 证书路径
    
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
        ssl_ecdh_curve secp384r1;
        ssl_session_timeout 10m;
        ssl_session_cache shared:SSL:10m;
        ssl_session_tickets off;
        ssl_stapling on;
        ssl_stapling_verify on;
    
        # 重定向所有不带 www 的 HTTPS 请求到 www.lwbj.cn
        return 301 https://lwbj.cn$request_uri;
    }
    
    # 处理带 www 的 HTTPS 请求
    server {
        listen 443 ssl http2;
        root /var/www/www.lwbj.cn; # 确保这里是正确的根目录路径
        index index.html index.htm index.php index.nginx-debian.html;
    
        server_name  www.lwbj.cn;
        client_max_body_size 20m;
    
        ssl_certificate /etc/nginx/ssl/www.lwbj.cn.pem; # 确保证书路径正确
        ssl_certificate_key /etc/nginx/ssl/www.lwbj.cn.net.key; # 确保证书路径正确
    
        #加上TLSv1,HTTPS检测会报PCI DSS不合规
        ssl_protocols  TLSv1.2 TLSv1.3;# Requires nginx >= 1.13.0 else use TLSv1.2
        ssl_prefer_server_ciphers on;
        ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
        ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
        ssl_session_timeout 10m;
        ssl_session_cache shared:SSL:10m;
        ssl_session_tickets off; # Requires nginx >= 1.5.9
        ssl_stapling on; # Requires nginx >= 1.3.7
        ssl_stapling_verify on; # Requires nginx => 1.3.7
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        }
    }
  • Debian+Nginx+PHP配置模板

    Nginx+PHP+HTTP的配置模板,如果要部署其它网站,可以基于此进行修改:

    server {
        listen 80;
        root /var/www/www.example.com;
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.php index.nginx-debian.html;
    
        server_name www.example.com example.com;
    
        client_max_body_size 20m;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    
            # With php-fpm (or other unix sockets):
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        }
    }
  • Nginx反向代理配置模板

    每次配置nginx反向代理时,都要查一次资料,现在记录在这里,备忘、备查。

    server {
        listen 80;
        server_name www.example.com;
        location /{
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8080;
        }
    }
  • 解决 Nginx SSL 证书报错

    闲来无事,查看了一下 nginx 的 error.log 日志文件,发现里面大量的报错信息:cannot load certificate “data:”: PEM_read_bio_X509_AUX() failed (SSL: error:0909006C:PEM routines:get_name:no start line:Expecting: TRUSTED CERTIFICATE) while SSL handshaking…

    是 ssl 证书报错了,想了一下,整个服务器只有这个博客使用了 https ,理所当然的从博客的 nginx 配置文件和证书文件入手,历时三天也没找到原因,甚至一度把证书都更换了,从腾讯云的 SSL 证书换到了阿里云的 SSL 证书,结果还是报错,说明证书没问题。

    就在快要放弃的时候,突然想到,之前为了屏蔽直接从 ip 来的请求,加了一些配置,屏蔽了http://iphttps://ip两种访问形式,而屏蔽 https+ip 也涉及到了 SSL 证书的问题,当时是通过 map 映射给了一个空证书,会不会是这个原因?

    于是,为了验证这个猜想,把博客站点的错误日志和其它站点的分开,单独统计。过了一夜,第二天打开日志文件查看,果然,博客的错误日志中并没有此错误,只有 error.log 里面有,这就说明错误不是博客站点的 SSL 证书引起的,那么只可能是之前屏蔽 https+ip 的配置引起的。

    找到了原因,那么解决起来就快了。

    先帖一下之前的配置:

    map "" $empty {
        default "";
    }
    server {
        listen 80 default_server;
        listen 443 ssl http2 default_server;
        listen [::]:80 default_server;
        listen [::]:443 ssl http2 default_server;
        server_name _;
    
        ssl_ciphers aNULL;
        ssl_certificate data:$empty;
        ssl_certificate_key data:$empty;
        return 444;
    }

    把上面的配置改成如下:

    # 禁止直接通过IP访问网站
    server {
        listen 80 default_server;
        server_name _;
        return 444;
    }

    删除了屏蔽 https+ip 的配置,只保留屏蔽 http+ip 的配置。然后在博客站点的 nginx 配置文件中添加如下代码:

    # 通过 default_server 把博客站点设置为默认的 https 站点
    listen 443 ssl http2 default_server;
    
    if ($host != 'wujie.me') {
        rewrite ^/(.*)$ https://wujie.me/$1 permanent;
        break;
    }

    把直接从非域名来的请求(包括从 ip 来的请求)跳转到域名就可以了。

    这样修改后,经过两天的观察,没有再出现错误,问题解决!

  • PHP-FPM和Nginx使用Unix Domain Socket通讯

    第一步,创建 unix domain sock 文件

    cd /run
    mkdir php && cd $_
    touch php7.4-fpm.sock
    chown www-data:www-data php7.4-fpm.sock
    chmod 777 ./php7.4-fpm.sock

    第二步,配置 php-fpm

    cd /usr/local/php/php74/etc/php-fpm.d
    vi www.conf

    listen = 127.0.0.1:9074 改为 listen = /run/php/php7.4-fpm.sock

    保存后,执行systemctl restart php7.4-fpm重启 php-fpm 。

    第三步,配置 nginx

    fastcgi_pass 127.0.0.1:9074;改为fastcgi_pass unix:/run/php/php7.4-fpm.sock;

    保存后,执行nginx -s reload使 nginx 配置生效。

    PS. 可以把 Unix Domain Socket 文件放到 /dev/shm 下以提高性能。因为这个目录不在硬盘上,而是在内存里。

    PS. 这么改完之后,一定记得所有用到这个 PHP-FPM 的站点,nginx 配置都要做如上修改,不然网站就访问不了了。

  • nginx 做302 永久跳转

    例如:example.com 跳转到 www.example.com

    server {
        listen 80;
        server_name example.com;
        rewrite ^/(.*)$ http://www.example.com/$1 redirect;
        access_log off;
    }