Centos 7 nginx 的详细配置

2019-07-26  本文已影响0人  蟠龙有悔

由于之前是装的apache的环境,现在想换成nginx

nginx环境之前已经安装过这里略过

修改 nginx 配置文件 nginx.conf

http{
    fastcgi_buffer_size 512k;
    fastcgi_buffers 8 512k;
    fastcgi_busy_buffers_size 512k;
    fastcgi_temp_file_write_size 512k;
    ...
}

nginx.confhttp模块加上这一段代码,这是由于nginx用于接收请求头的buffer太小,会在错误日志中报:*36 an upstream response is buffered to a temporary file /var/cache/nginx/fastcgi_temp/2/00/0000000002类似的错误,加上上面代码可以解决

添加域名模块

在/etc/nginx/conf.d/ 下添加 文件 www.domain.com.conf,复制default.conf中的文件,修改的内容如下:

server {
    listen       80;
    server_name  www.domain.com;

    location / {
        root   /var/www/html;
        index  index.html index.htm index.php;
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?s=$1 last;
        }
    }
    location ~ \.php$ {
       root           /var/www/html;
       # fastcgi_pass   127.0.0.1:9000;
       fastcgi_pass   unix:/run/php-fpm/php7.2-fpm.sock;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
    }
}

fastcgi_pass 这里使用unix domain socket连接套接字,对应的 也要在 /etc/php-fpm.d/www.conf 监听的地址保持一致 listen = /run/php-fpm/php7.2-fpm.sock
默认 没有/run/php-fpm/php7.2-fpm.sock这个文件,重启php-fpm后会生成.

需要注意的是,由于 php7.2-fpm.sock 这个文件是后面生成的,所以用户组和nginx不一致,会导致权限拒绝的bug,在nginx日志中可以查看到错误日志如下:connect() to unix:/run/php-fpm/php7.2-fpm.sock failed (13: Permission denied) while connecting to upstream...

增补:

接上面,这种情况一般在网页上表现为502错误,究其原因是 php-fpm 配置文件没有指定用户,默认给了root的用户和运行环境的用户不匹配,在/etc/php-fpm.d/www.conf配置如下:

...
listen.owner = nginx
listen.group = nginx

这里我运行环境的用户和用户组都是nginx所以如上配置

配置https

在云服务上购买一份SSL证书(有免费的,当前所用的就是免费的),审核通过后,下载,选nginx版的ssl配置,上传到 /etc/nginx/conf.d 目录下,拷贝一份default.conf 文件重命名为443domain.com.conf,配置如下:

server {
    listen 443 ssl; #SSL 访问端口号为 443
    server_name domain.com; #填写绑定证书的域名
    # ssl on;                   #启用 SSL 功能  ---- nginx 1.15+日志会提示 用 listen 443 ssl 替换 listen 443 + ssl on
    ssl_certificate /etc/nginx/conf.d/1_mamahaha.top_bundle.crt;        #证书文件名称
    ssl_certificate_key /etc/nginx/conf.d/2_mamahaha.top.key;       #私钥文件名称
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;            #请按照这个协议配置
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #请按照这个套件配置,配置加密套件,写法遵循 openssl 标准。
    ssl_prefer_server_ciphers on;
    location / {
            root /var/www/html;
            index  index.html index.htm index.php;
            if (!-e $request_filename) {
                rewrite ^/(.*)$ /index.php?s=$1 last;
            }
        }
         ...
}

这里用的是腾讯云,有提供相应的https配置教程,不过nginx的版本需要部分修改一下,不然容易出错

上一篇下一篇

猜你喜欢

热点阅读