Linux安装nginx含ssl(https)配置(亲测)

安装nginx相关依赖软件

1.选定源码目录
选定目录 /usr/local/ 
cd /usr/local/

2.安装PCRE库

yum -y install pcre-devel

3.安装zlib库

cd /usr/local/
wget http://www.zlib.net/zlib-1.2.11.tar.gz    
tar -xvf zlib-1.2.11.tar.gz 
cd zlib-1.2.11
./configure
make
make install

4.安装ssl

cd /usr/local/
wget https://www.openssl.org/source/openssl-1.0.2t.tar.gz
tar -xvf openssl-1.0.2t.tar.gz 
cd openssl-1.0.2t
./config
make
make install

5.安装nginx
Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤: 
cd /usr/local/
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -xvf nginx-1.14.0.tar.gz 
 cd nginx-1.14.0/
./configure --prefix=/usr/local/nginx
make
make install

  使用 /usr/local/nginx/sbin/nginx -t可查看应用nginx.conf的位置

6.the HTTP rewrite module requires the PCRE library报错

./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解决问题

 yum -y install pcre-devel
出现以下错误
 
src/http/ngx_http_script.c:698:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]
  698 |     code->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
      |                  ^
src/http/ngx_http_script.c: In function ‘ngx_http_script_add_var_code’:
src/http/ngx_http_script.c:787:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]
  787 |     code->code = (ngx_http_script_code_pt) ngx_http_script_copy_var_len_code;

vim /usr/local/nginx-1.14.0/objs/Makefile

 

 去掉-Werror 然后在重新make

 
7.启动
确保系统的 80 端口没被其他程序占用
/usr/local/nginx/sbin/nginx

  检查是否启动成功:

netstat -ano|grep 80 有结果输入说明启动成功

打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

  8.重启

/usr/local/nginx/sbin/nginx –s reload

9.修改配置文件

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

 nginx.conf内容

user  root; #这步是防止静态资源权限提示503
worker_processes  1;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;
    underscores_in_headers on; 
    sendfile        on;
    keepalive_timeout  65;
    client_max_body_size  80m;

    include  vhost/*.conf; 

}

我在conf目录创建vhost文件夹,专门放各个项目或者域名的配置,所以这里用到include  vhost/*.conf; 

cd vhost
vi test.conf
server {
        listen       80;
        server_name  www.test.com;

        server_name_in_redirect off;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        fastcgi_connect_timeout 1200;

        location /test-admin {

            #解决跨域问题

            add_header Access-Control-Allow-Methods *;
            add_header Access-Control-Max-Age 3600;
            add_header Access-Control-Allow-Credentials true;

            add_header Access-Control-Allow-Origin $http_origin;#获取请求端的origin值
            add_header Access-Control-Allow-Headers $http_access_control_request_headers;#这里的请求变量必须都是小写的
            if ($request_method = OPTIONS){#这里的if后面要带空格的,不然语法会错
              return 200;
             }

           proxy_pass http://127.0.0.1:6001/test-admin;                                             
        }

        location /test-file/ {
           alias  /home/test/test-file/;
        }

     #vue项目前端访问配置

          location /test-h5 {
            alias /home/test/app/test/dist;
            index index.html;
            try_files $uri $uri/ /index.html last;
           }


}

#如无需配置SSL证书,请忽略以下配置 server { listen
443; server_name www.test.com; #填写绑定证书的域名 ssl on; #1.21.5版本需将该行注释 ssl_certificate ssl/5334682_www.test.com.pem; # hps文件夹下的.crt文件 ssl_certificate_key ssl/5334682_www.test.com.key; #hps文件夹下的.key文件 ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照这个套件配置 ssl_prefer_server_ciphers on; location /test-admin { proxy_pass http://127.0.0.1:6001/test-admin; } location /test-file/ { alias /home/test/test-file/; } }

#现在nginx新版SSL配置,如下:
server {
        listen 443 ssl;
        server_name www.test.com; #填写绑定证书的域名
        ssl_certificate ssl/5334682_www.test.com.pem; # hps文件夹下的.crt文件
        ssl_certificate_key ssl/5334682_www.test.com.key; #hps文件夹下的.key文件
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照这个套件配置
        ssl_prefer_server_ciphers on;

            location /test-admin {
           proxy_pass http://127.0.0.1:6001/test-admin;                                             
        }

        location /test-file/ {
           alias  /home/test/test-file/;
        }
    }
 

这里我配置SSL证书,在conf目录下创建ssl文件夹,专门储存申请的SSL证书

 

如无需配置SSL证书,请忽略以下步骤

由于我们未装SSL模块,启动时,会提示nginx:[emerg]unknown directive ssl错误

因为我们配置这个SSL证书需要引用到nginx的中SSL这模块,然而我们一开始编译的Nginx的时候并没有把SSL模块一起编译进去,所以导致这个错误的出现。

解决方案:

步骤一:我们先来到当初下载nginx的包压缩的解压目录,如果你是看小编写的教程安装的,解压目录应该在“/usr/loacl/nginx-1.14.0/”,绝大多数应该都是在这个目录下的。

步骤二:来到解压目录下后,按顺序执行一下命令: 

 命令1、./configure --with-http_ssl_module  //重新添加这个ssl模块

  注意如果没有出现错误,则直接看命令2即可 
   执行以上一条命令出现这个错误(./configure:错误:SSL模块需要OpenSSL库。),原因是因为缺少了OpenSSL,那我们再来安装一个即可执行:yum -y install openssl openssl-devel 
  等待OpenSSL的安装完成后,再执行./configure --with-http_ssl_module即可
注:如果还提示OpenSSL未安装,可参考该文章下载源码安装https://blog.csdn.net/qq_29235677/article/details/122083300,原理就是下载源码安装,在./configure编译带上OpenSSL源码路径,例如
./configure --with-http_ssl_module --with-openssl=/root/openssl-1.1.1g,这里/root/openssl-1.1.1g看你源码放在哪里
 命令2、执行make命令,但是不要执行make install,因为make是用来编译的,而make install是安装,不然你整个nginx会重新覆盖的。
 命令3、在我们执行完做命令后,我们可以查看到在nginx解压目录下,objs文件夹中多了一个nginx的文件,这个就是新版本的程序了。首先我们把之前的nginx先备份一下,然后把新的程序复制过去覆盖之前的即可。
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp objs/nginx /usr/local/nginx/sbin/nginx

 

 命令4,最后我们来到Nginx安装目录下,来查看是否有安装ssl模块成功。执行./sbin/nginx -V即可 

 

出现Nginx的405 not allowed错误解决【Nginx静态文件响应POST请求,提示405错误问题】

可参考改文章解决

https://www.zuopeng.gd.cn/article/details/id/116.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2021-03-16 15:56  灰辉-灰  阅读(5118)  评论(0编辑  收藏  举报