title: Ubuntu-Nginx-配置
date: 2018-07-01 16:01:33
categories:

  • Linux
  • Linux
    tags: [linux,linux]

安装环境及版本:

  • 系统:ubuntu 18.04 LTS
  • Nginx: 1.16.0

一 安装

  1. ubuntu 18.04
    1. #更新软件源
    2. sudo apt update
    3. #安装
    4. sudo apt install nginx
    5. #命令启动、停止、重启
    6. service nginx start|stop|reload
  1. centos

访问http://localhost

二 代理转发

  • 默认配置文件路径: vim /etc/nginx/nginx.conf
  • 检查配置文件中语法是否正确: nginx -t
  1. http代理配置

    server {
     listen       80;    #监听端口
     server_name  localhost;    #监听地址
    
     location / {
         #若是docker中,需将localhost改为宿主机的ip
         proxy_pass  http://localhost:8081/;
     }
     location /jian {
         proxy_pass  https://www.jianshu.com/;
     }
    }
    


代理说明:
访问 http://localhost:80 实际是:https://localhost:8081
访问 http://localhost/jian 实际是:https://www.jianshu.com

  1. https代理配置
    首先生成证书和私钥,进入/etc/nginx路径,跟nginx.conf同级路径
    #创建服务器证书密钥文件 server.key(需要设置密码 eg:1234)
    openssl genrsa -des3 -out server.key 2048
    #去除文件密码
    openssl rsa -in server.key -out server.key
    #3.创建服务器证书的申请文件 server.csr(全部默认即可)
    openssl req -new -key server.key -out server.csr
    #4.生成证书文件server.crt
    openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
    


配置证书

server {
    listen       443 ssl;
    server_name  127.0.0.1;

    # ssl证书地址
    ssl_certificate     server.crt;  #pem文件的路径
    ssl_certificate_key server.key;  #key文件的路径

    # ssl验证相关配置
    ssl_session_timeout  5m;            #缓存有效期
    ssl_session_cache    shared:SSL:1m;    #session缓存

    ssl_ciphers    HIGH:!aNULL:!MD5;        #加密算法
    ssl_protocols SSLv2 SSLv3 TLSv1;    #安全链接可选的加密协议
    ssl_prefer_server_ciphers on;        #使用服务器端的首选算法

    location / {
        #不限制下载文件大小
        proxy_max_temp_file_size 0; 
        proxy_pass  http://127.0.0.1:8080/;
    }
}


访问地址: https://localhost

  1. 文件服务器

    server {
     listen       80;
     server_name  _;
    
     location /ftp {
         autoindex on;             #开启索引功能
         autoindex_exact_size off; #关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
         autoindex_localtime on;   #显示本机时间而非 GMT 时间
         charset utf-8;               #避免中文乱码
         alias   /opt/package/;      #文件根路径地址
     }
    }
    


访问地址: http://localhost/ftp, 浏览/opt/package 路径下文件

  1. location正则写法 ```shell
  • 已=开头表示精确匹配
  • ^~ 开头表示uri以某个常规字符串开头,不是正则匹配
  • ~ 开头表示区分大小写的正则匹配;
  • ~* 开头表示不区分大小写的正则匹配
  • / 通用匹配, 如果没有其它匹配,任何请求都会匹配到 ```


location正则详解:https://segmentfault.com/a/1190000002797606

  1. proxy_pass 转发相对—绝对路径
    在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;
    如果没有/,则会把匹配的路径部分也给代理走
    例:localhost/proxy/test.html 进行访问
    location /proxy/ {
     proxy_pass http://127.0.0.1:81/;
    }
    #转发到:localhost/test.html
    
location /proxy/ {
    proxy_pass http://127.0.0.1:81; #少一个 /
}
转发到:localhost/proxy/test.html

三 卸载

  1. ubuntu
    #首先停止nginx 服务
    service nginx stop
    #删除nginx
    apt --purge remove nginx
    #自动移除全部不使用的软件包
    apt autoremove
    #列出与nginx相关的软件 并删除显示的软件
    dpkg --get-selections|grep nginx
    apt-get --purge remove nginx
    
  1. centos