在CentOS中安裝和使用nginx


概述

本文簡單講述一下,如何快速將一個內網的Web服務通過nginx提供給外網訪問,並且啟用HTTPS。例如我們部署了一個kubesphere,地址為192.168.202.151:30880,需要通過nginx來提供給外網訪問,我們來看看在CentOS上如何進行快速部署。

安裝

首先我們通過yum來安裝nginx相關的服務

yum install -y nginx

安裝完畢之后,我們啟動nginx,並檢查nginx狀態

# 啟動nginx
systemctl start nginx

# 查看nginx服務狀態
systemctl status nginx

服務正常運行之后,接下來我們創建一個目錄,將我們的證書文件放入其中,個人使用可以從阿里雲或者騰訊雲等雲平台申請免費的證書

# 創建目錄存放證書,將證書文件復制到里面,示例證書文件為:mstmdev.com_bundle.crt  mstmdev.com.key
mkdir /etc/nginx/ssl_cert/

證書准備完畢之后,我們開始最后一步,編輯我們的nginx配置文件

vim /etc/nginx/nginx.conf

以下為修改后的配置文件,目的是將我們服務器上的http請求全部重定向到https上,並將https上的請求轉發到內網服務192.168.202.151:30880之中,讓我們可以通過外網域名訪問我們的服務。

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

     upstream kubesphere{
      server 192.168.202.151:30880;
     }
#    server {
#        listen       80;
#        listen       [::]:80;
#        server_name  _;
#        root         /usr/share/nginx/html;

#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;

#        error_page 404 /404.html;
#        location = /404.html {
#        }

#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

    server {
       listen     80;
       server_name  mstmdev.com;
       access_log  /var/log/nginx/mstmdev_com_access.log  main;
       # 將http請求重定向為https
       rewrite ^(.*)$ https://$host$1 permanent;
    }

# Settings for a TLS enabled server.
#
    server {
        listen       443 ssl http2;
        listen       [::]:443 ssl http2;
        server_name  gors.cc;
#        root         /usr/share/nginx/html;
#
        # 這里就是配置我們剛才存放的證書文件的路徑
        ssl_certificate "/etc/nginx/ssl_cert/mstmdev.com_bundle.crt";
        ssl_certificate_key "/etc/nginx/ssl_cert/mstmdev.com.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }

       access_log  /var/log/nginx/https_mstmdev_com.log  main;
       # 將https請求轉發到我們的內網服務192.168.202.151:30880中
       location / {
        proxy_pass http://kubesphere/;
       }
    }

}

配置文件修改完畢之后,重啟nginx,使配置生效

systemctl restart nginx

此時我們將自己的域名的DNS解析到我們服務器的公網IP地址就可以在外網通過HTTPS地址訪問kubesphere后台了。

如果想要在服務器重啟后自動啟動nginx,可以將其設置為開機啟動

systemctl enable nginx

如果不再需要開機啟動,將其禁用即可

systemctl disable nginx

更多詳細的配置可以參考官方的文檔:https://nginx.org/en/docs/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM