CentOS8.0编译安装NGINX1.17.4

下载NGINX1.17.4:

wget http://nginx.org/download/nginx-1.17.4.tar.gz

解压缩然后进入源码目录:

tar zxf nginx-1.17.4.tar.gz
cd nginx-1.17.4

编译安装:

./configure --prefix=/usr/local/nginx --user=nobody --group=nobody --with-http_ssl_module --with-http_v2_module && make && make install

出现报错信息:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

这是因为缺少pcre-devel,安装pcre-devel:

yum install pcre-devel

编辑nginx.conf配置文件:

vi /usr/local/nginx/conf/nginx.conf

支持IPv6:

#HTTP
listen  [::]:80;
#HTTPS
listen  [::]:443 ssl;

启用HTTPS和HTTP2以及HSTS:

server {
        listen       443 ssl http2;
        listen  [::]:443 ssl http2;
        server_name  你的域名;
        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

        ssl_certificate      你的证书路径;
        ssl_certificate_key  你的私钥路径;
        ssl_session_cache    shared:SSL:5m;
        ssl_session_timeout  5m;
        ssl_early_data       on;
        ssl_protocols        TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers  on;
}

使HTTP请求301跳转至HTTPS:

server {
        listen      80;
        listen [::]:80;
        server_name 你的域名;
        return 301 https://你的域名;
    }

NGINX的WordPress伪静态规则:

location / {
            root   html;
            index  index.php;
            if (-f $request_filename/index.html){
                rewrite (.*) $1/index.html break;
            }
            if (-f $request_filename/index.php){
                rewrite (.*) $1/index.php;
            }
            if (!-f $request_filename){
                rewrite (.*) /index.php;
            }
        }

支持PHP:

location ~ \.php$ {
            root           html;
            fastcgi_pass   unix:/dev/shm/php-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  HTTPS            $https;
            include        fastcgi_params;
        }

浏览器缓存文件类型和过期时间以及防盗链:

location ~ \.(gif|jpg|png|css|js)$ {
            root   html;
            expires 30d;
            valid_referers none blocked 你的域名;
            if ($invalid_referer) {
            return 403;
            }
        }

启动NGINX:

/usr/local/nginx/sbin/nginx

加入对话

3条评论

留下评论

电子邮件地址不会被公开。 必填项已用*标注

回到顶部