windows環境下Nginx部署及Https設置


一、Nginx安裝部署及常用命令。

 1.1、其實Nginx是免安裝的。直接在官網下載zip包,解壓即可,下載地址:http://nginx.org/en/download.html,因為我這邊的開發服務器是阿里雲windows服務器,所以我下載的是對應的windows版本(當時下載的是1.15.9版本)

下載完成解壓即可。

1.2   Nginx基本配置,配置文件路徑: 安裝路徑下的conf文件夾下的nginx.conf。Nginx默認啟動端口是80,在服務器上注意不要和tomcat沖突

     負載均衡配置如下:

     這里,我配置了兩個項目,分別命名為:boot和product,示例配置只配置了boot項目的訪問,其實配置第二個時候和第一個一樣,只需再復制一個service,端口也是80,修改service_name(可指定為具體域名)及proxy_pass為product項目

http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream  boot {  #服務器集群名字   
      ip_hash; #同一個用戶指定訪問一台服務,保證session可用
      server  192.168.20.122:8090  weight=1; #weight設置訪問權重,值越大,訪問概率越高
      server  192.168.20.101:8090  weight=1; 
    } 
    upstream  product {  #可以指定多個項目 
      server  192.168.20.122:8099 weight=1; 
    } 

  server {
        listen       80;
        server_name  localhost;#此處可以設置為項目域名如:www.baidu.com

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

 
        location / {
#代理轉向到那個項目 proxy_pass http:
//boot; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }

 

1.2、Nginx常用命令

      我這里的解壓路徑是 C:\Program Files\nginx,以下操作都是基於此路徑。

     1、啟動

         cd C:\Program Files\nginx\nginx-1.15.9nginx.conf
         nginx.exe

     2、刷新(重新加載)

        nginx -s reload

     3、停止(有時候該命令並不好用,可以直接在任務管理器,進程中找到nginx.exe並結束)

       nginx -s quit

 

二、Nginx下Https設置

1、https證書獲取有很多途徑,我這里使用的阿里雲提供的免費證書(每個阿里雲 賬號可免費申請20個Https證書)。登陸阿里雲平台,搜索ssl,購買證書如下:

 

購買完成后跳轉管理頁面:

購買完成后下個流程是驗證,這里沒有截圖直接跳過了,驗證頁面最主要的信息是綁定域名,其他的可以按照默認設置,聯系方式填下,按照指引逐步操作,最后等待審核通過就行了,一般審核很快(半個小時以內)。

 

審核完成后,選擇購買的ssl,點擊下載,選擇Nginx,剩下的就按照阿里雲幫助文檔配置就行了

 

 

 證書下載完成后,把里面的pem和key文件復制,然后在Nginx安裝目錄下的conf文件目錄下新建文件夾cert,並將證書文件復制進去

准備工作完成后,最后配置Nginx配置文件nginx.conf,刪除原有的service,添加如下配置

server {
       
        listen 443 ssl;
        server_name www.baidu.com; #修改為申請證書綁定的域名
        
        root html;
        index index.html index.htm;
        ssl_certificate   cert/XXX.pem;
        ssl_certificate_key  cert/XXX.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 / {
                proxy_pass http://boot; 
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        
    }

    server {

        listen       80;
        server_name  www.baidu.com; #修改為申請證書綁定的域名
        rewrite ^(.*)$ https://${server_name}$1 permanent; 
    }

注:在阿里雲ECS上部署的使用,一定要去安全組規則里面打開443端口(默認是關閉的),否則是無法使用https的

 

通過上面配置,則項目只能通過https訪問(http會重定向到https),如果向同時使用https和http,則刪除第二個service(監聽80端口並控制重定向),同時在第一個service 的listen 443 ssl前面加上listen 80 default backlog=2048;

這樣則完成了http和https的並行訪問。

 

總結:如果有多個項目需要用Nginx代理,則復制上面的service,修改證書(如果是https的話)及項目(proxy_pass)。

 


免責聲明!

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



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