Nginx——ubuntu安装Nginx并配置https

一、需求

介绍: 我们在部署应用时,希望用到 Nginx ,并配置上 https 。我在网上看了很多文章,感觉都不是很系统。因此写下此文,以备日后使用。

二、安装 Nginx

  1. 获取安装包
    1. wget http://nginx.org/download/nginx-1.17.10.tar.gz
  2. 解压
    1. tar -zxvf nginx-1.17.10.tar.gz
  3. 配置 with-http_ssl_moudle。很关键
    1. ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module

    不过直接直接会报错

    1. ./configure: error: invalid option "--with-http_ssl_moudle"

    需要安装依赖包

    1. apt-get install gcc
    2. apt-get install libpcre3 libpcre3-dev
    3. apt-get install zlib1g zlib1g-dev
    4. # Ubuntu14.04的仓库中没有发现openssl-dev,由下面openssl和libssl-dev替代
    5. #apt-get install openssl openssl-dev
    6. sudo apt-get install openssl
    7. sudo apt-get install libssl-dev
    8. sudo apt-get install libpcre3 libpcre3-dev

    然后再运行上面的那一句

  4. 安装
    1. make
    2. make install

三、配置 https

  1. 将 https 的证书存放在 /usr/local/nginx/conf 目录下,我是新建了一个文件夹 cert
    20200518001354418-1
  2. 编辑配置文件 /usr/local/nginx/conf/nginx.conf 如下:
    1. server {
    2. listen 80;
    3. server_name 你的域名;
    4. return 301 https://你的域名$request_uri;
    5. #charset koi8-r;
    6. #access_log logs/host.access.log main;
    7. location / {
    8. proxy_pass http://127.0.0.1:8080;
    9. proxy_set_header Host $host;
    10. proxy_set_header X-Real-IP $remote_addr;
    11. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    12. proxy_set_header X-Forwarded-Proto $scheme;
    13. proxy_set_header X-Forwarded-Port $server_port;
    14. }
    15. }
    16. server {
    17. listen 443 ssl;
    18. server_name 你的域名;
    19. ssl_certificate cert/你的证书crt;
    20. ssl_certificate_key cert/你的证书key;
    21. #ssl_session_cache shared:(解析问题,去除改括号)SSL:1m;
    22. #ssl_session_timeout 5m;
    23. #ssl_ciphers HIGH:!aNULL:!MD5;
    24. ssl_session_timeout 24h;
    25. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:(解析问题,去除改括号)ECDHE:(解析问题,去除改括号)ECDH:(解析问题,去除改括号)AES:(解析问题,去除改括号)HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    26. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    27. ssl_prefer_server_ciphers on;
    28. location / {
    29. proxy_pass http://127.0.0.1:8080;
    30. proxy_set_header Host $host;
    31. proxy_set_header X-Real-IP $remote_addr;
    32. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    33. proxy_set_header X-Forwarded-Proto $scheme;
    34. proxy_set_header X-Forwarded-Port $server_port;
    35. }
    36. location /pic {
    37. proxy_pass http://127.0.0.1:8081;
    38. proxy_set_header Host $host;
    39. proxy_set_header X-Real-IP $remote_addr;
    40. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    41. proxy_set_header X-Forwarded-Proto $scheme;
    42. proxy_set_header X-Forwarded-Port $server_port;
    43. }
    44. }

四、SpringBoot 配置使用

  1. 配置如下:
    1. server:
    2. port: 8080
    3. tomcat:
    4. remote-ip-header: x-forwarded-for
    5. protocol-header: x-forwarded-proto
    6. port-header: X-Forwarded-Port
    7. use-forward-headers: true
 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载:小林博客 www.vvso.cn小林博客 » Nginx——ubuntu安装Nginx并配置https

分享到: 更多 (0)

评论 抢沙发

切换注册

登录

忘记密码 ?

切换登录

注册