1. 安装
首先要保证机器上有nginx, 以Ubuntu 为例,用下面的命令安装:
sudo apt-get install nginx
关于nginx在不同版本操作系统上默认配置文件的位置和详细配置说明。
参阅官方文档 http://nginx.org/en/docs/
http://nginx.org/en/docs/http/request_processing.html
2. 配置文件
对于Ubuntu系统, 安装后默认的配置文件在 /etc/nginx/nginx.conf
#By default, it has lines below.
#It is recommend that we put user config file there.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
3. 简单文件服务器的配置文件:
支持http/https, 根据文件类型下载或预览。
aautoindex on;
autoindex_exact_size on;# 显示文件大小
autoindex_localtime on; #之类的参数写这里
charset utf-8;
server {
listen 443 default_server;
listen [::]:443 default_server;
server_name _;
ssl on;
root /var/log; #前台文件存放文件夹,可改成别的
ssl_certificate cert/server.crt; #改成你的证书的名字
ssl_certificate_key cert/server.key; #你的证书的名字
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
if ($request_filename ~* ^.*?.(doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|jpg|png)$){
add_header Content-Disposition: attachment;
}
if ($request_filename ~* ^.*?.(c|cpp|h|java|py|log)$){
add_header Content-Type: 'text/html; charset=utf-8';
}
if ($request_filename ~* ^.*?$){
add_header Content-Type: 'text/html; charset=utf-8';
}
}
}
server {
listen 8080 default_server;
listen [::]:8080 default_server;
server_name _;
root /var/log;
location / {
if ($request_filename ~* ^.*?.(doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|jpg|png)$){
add_header Content-Disposition: attachment;
}
if ($request_filename ~* ^.*?.(c|cpp|h|java|py|log)$){
add_header Content-Type: 'text/html; charset=utf-8';
}
if ($request_filename ~* ^.*?$){
add_header Content-Type: 'text/html; charset=utf-8';
}
}
}
4. 如何生成证书
使用 OpenSSL 生成 SSL Key 和 CSR 文件
配置 HTTPS 要用到私钥 example.key 文件和 example.crt 证书文件,申请证书文件的时候要用到 example.csr 文件,OpenSSL
命令可以生成 example.key 文件和 example.csr 证书文件。
- CSR:Cerificate Signing Request,证书签署请求文件,里面包含申请者的 DN(Distinguished Name,标识名)和公钥信息,在第三方证书颁发机构签署证书的时候需要提供。证书颁发机构拿到 CSR 后使用其根证书私钥对证书进行加密并生成 CRT 证书文件,里面包含证书加密信息以及申请者的 DN 及公钥信息
- Key:证书申请者私钥文件,和证书里面的公钥配对使用,在 HTTPS 『握手』通讯过程需要使用私钥去解密客戶端发來的经过证书公钥加密的随机数信息,是 HTTPS 加密通讯过程非常重要的文件,在配置 HTTPS 的時候要用到
使用 OpenSSl
命令可以在系统当前目录生成 example.key 和 example.csr 文件:
openssl req -new -newkey rsa:2048 -sha256 -nodes -out example_com.csr -keyout example_com.key -subj "/C=CN/ST=ShenZhen/L=ShenZhen/O=Example Inc./OU=Web Security/CN=example.com"
C:Country ,单位所在国家,为两位数的国家缩写,如: CN 就是中国下面是上述命令相关字段含义:
- ST 字段: State/Province ,单位所在州或省
- L 字段: Locality ,单位所在城市 / 或县区
- O 字段: Organization ,此网站的单位名称;
- OU 字段: Organization Unit,下属部门名称;也常常用于显示其他证书相关信息,如证书类型,证书产品名称或身份验证类型或验证内容等;
- CN 字段: Common Name ,网站的域名;
生成 csr 文件后,提供给 CA 机构,签署成功后,就会得到一個 example.crt 证书文件,SSL 证书文件获得后,就可以在 Nginx 配置文件里配置 HTTPS 了。
Reference: https://aotu.io/notes/2016/08/16/nginx-https/index.html
5. 测试时如果发现外部访问有问题,优先考虑是 SELinux 和 iptables 的问题。
6. Apply ngx-fancy-index 使的文件服务器的访问页面美观已经有新增的功能
default.conf
server {
listen 8080 default_server;
listen [::]:8080 default_server;
server_name _*;
root /home/liyang;
include fancyindex.conf;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
fancyindex.conf
fancyindex on;
fancyindex_localtime on;
fancyindex_exact_size off;
fancyindex_header "/Nginx-Fancyindex-Theme-dark/header.html";
fancyindex_footer "/Nginx-Fancyindex-Theme-dark/footer.html";
fancyindex_ignore "examplefile.html"; # Ignored files will not show up in the directory listing, but will still be public.
fancyindex_ignore "Nginx-Fancyindex-Theme-light"; # Making sure folder where files are don't show up in the listing.
# Warning: if you use an old version of ngx-fancyindex, comment the last line if you
# encounter a bug. See https://github.com/Naereen/Nginx-Fancyindex-Theme/issues/10
fancyindex_name_length 255; # Maximum file name length in bytes, change as you like.