首页 > 系统相关 >ubuntu18.04安装nginx并支持https和文件上传

ubuntu18.04安装nginx并支持https和文件上传

时间:2023-01-16 14:36:23浏览次数:53  
标签:key upload server nginx usr https ubuntu18.04 local

在ubuntu10.4server下安装nginx并支持上传和https功能

环境:ubuntu18.04server
在root权限下操作

1. 安装编译环境

cd
apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev build-essential unzip


2. 下载并解压nginx源码

wget http://nginx.org/download/nginx-1.15.8.tar.gz
tar -zxvf nginx-1.8.1.tar.gz


3. 下载并解压上传模块

wget https://github.com/fdintino/nginx-upload-module/archive/refs/heads/master.zip
unzip master.zip

  

5. 移动nginx和上传模块到指定目录

mv nginx-1.8.1 /usr/local/nginx
mv nginx-upload-module-master /usr/local/nginx/

 

6. 生成服务器密钥和证书用于https,
6.1 进入openssl下执行以下命令

openssl

6.2 生成CA私钥

openssl> genrsa -des3 -out ca.key 1024

6.3 去除CA私钥密码

openssl> rsa -in ca.key -out ca.key

6.3 生成CA

openssl> req -new -x509 -key ca.key -out ca.crt -days 365

6.4 重新进入openssl生成用户证书等

openssl> exit
openssl

6.4 生成用户私钥

openssl> genrsa -des3 -out server.key 1024

6.5 去除用户私钥中的密码

openssl> rsa -in server.key -out server.key

6.5 生成证书申请文件

openssl> req -new -key server.key -out server.csr

6.6 通过CA生成证书

openssl> x509 -req -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt -days 365

6.7 需要的文件为用户私钥server.key和用户证书server.crt,将用户私钥和用户证书移动到nginx下目录CA中

mkdir -p /usr/local/nginx/CA
mv server.key /usr/local/nginx/CA
mv server.crt /usr/local/nginx/CA

7. 编译nginx

cd /usr/local/nginx
./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --add-module=./nginx-upload-module-master
make


8. 修改配置文件/usr/local/nginx/conf/nginx.conf,替换为以下内容

user root;
worker_processes  1;

pid        logs/nginx.pid;

events {
    worker_connections 1024;
}

http {
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /usr/local/nginx/conf/mime.types;
    default_type        application/octet-stream;

    server {
        client_max_body_size 100g;
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        autoindex   on;
        
        listen       443 ssl;
        listen       [::]:443 default_server;
        server_name  localhost;

        ssl_certificate      /usr/local/nginx/CA/server.crt;
        ssl_certificate_key  /usr/local/nginx/CA/server.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        
        
        location / {
            root /usr/local/nginx/html;
            autoindex on;
        }
        
        location /upload {
              
              upload_store /usr/local/nginx/html/upload;
              upload_store_access user:rw;
              upload_set_form_field $upload_field_name.name "$upload_file_name";
              upload_set_form_field $upload_field_name.content_type "$upload_content_type";
              upload_set_form_field $upload_field_name.path "$upload_tmp_path";
              upload_pass_form_field "^submit$|^description$";
              upload_pass @mock;
        }
        location @mock {
              return 200 "ok\r\n";
        }
    }
	
}

9. 创建上传文件夹支持文件上传功能
mkdir /usr/local/nginx/html/upload
chmod 777 /usr/local/nginx/upload/

10. 增加上传文件测试网页/usr/local/nginx/html/test_upload.html,内容如下
<html>
<head>
<title>Test upload</title>
</head>
<body>
<h2>Select files to upload</h2>
<form enctype="multipart/form-data" action="/upload" method="post">
<input type="file" name="file1"><br>
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>

11. 安装nginx
make install

12. 检查nginx配置文件正确性
/usr/local/nginx/sbin/nginx -t

13. 开启nginx
/usr/local/nginx/sbin/nginx

14. 测试文件上传,访问http://127.0.0.1/upload,上传文件之后在/usr/local/nginx/html/upload下是否有文件存在

15. 测试https,访问https://127.0.0.1/

16 测试http,访问http://127.0.0.1/


标签:key,upload,server,nginx,usr,https,ubuntu18.04,local
From: https://www.cnblogs.com/roverq/p/17055303.html

相关文章

  • nginx升级连接为websocket
    利用nginx代理wss【通讯原理及流程】wss协议实际是websocket+SSL,就是在websocket协议上加入SSL层,类似https(http+SSL)。客户端发起wss连接连到nginxnginx将wss协议的数......
  • https://github.com/Abraham423/CenterPointTensorRT 的cmake
    ​​link​​cmake_minimum_required(VERSION2.8.3)project(centerpoint)set(USE_CUDATrue)#ForTensorRTsamplelib#set(TRT_ROOT/home/wanghao/Desktop/projects/T......
  • 内网Linux下安装Nginx1.23,添加stream模块实现tcp/udp代理转发
    环境:centos7.6ngx_stream_core_module这个模块在1.9.0版本后将被启用。但是并不会默认安装,需要在编译时通过指定--with-stream参数来激活这个模块,window下并不支持udp......
  • Nginx与LUA(3)
    您好,我是湘王,这是我的51CTO博客,欢迎您来,欢迎您再来~在互联网应用中,很多场景都会涉及到高并发请求,如果不对这些请求做限制,那么服务器很快就会被挤垮。就像在12306买票一样,如果......
  • Nginx与LUA(3)
    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来~   在互联网应用中,很多场景都会涉及到高并发请求,如果不对这些请求做限制,那么服务器很快就会被挤垮。就像在12306买......
  • nginx配置安全漏洞之CRLF注入漏洞
    CRLF漏洞原理参考:https://www.leavesongs.com/PENETRATION/Sina-CRLF-Injection.htmlCRLF是”回车+换行”(\r\n)的简称。在HTTP协议中,HTTPHeader与HTTPBody是用两个CRL......
  • nginx开机启动脚本
    vi/etc/rc.d/init.d/nginx#编辑启动文件添加下面内容=======================================================#!/bin/sh##nginx-thisscriptstartsandstopst......
  • 使用docker中的nginx实现反向代理
    场景:宿主机有个目录存储静态资源,需要通过nginx代理出去,用户访问http://host:port/resource/xxxx访问。   1、修改宿主机中的/nginx/conf.d/default.conf文件(该文......
  • docker中离线安装nginx
    注:默认已经安装好docker。 为什么要离线安装?其实离线安装是建立在在线安装的基础上的;因为有可能我们的服务器由于安全问题无法访问外网,自此我们需要将镜像手动上传至服......
  • kubernetes安装ingress-nginx的步骤
    kubernetes安装ingress-nginx的步骤系统版本:CentOs8.1k8s版本:v1.21.3containerd版本:ctrcontainerd.io1.4.3一:准备yaml文件github仓库地址:wgethttps://raw.githubuse......