dotNet Core WEB程序使用 Nginx反向代理

以前記錄過一篇 使用 jexus 做爲dotNetCore的反向代理,發現jexus的內存佔用較大,最終選擇使用Nginx的緣由就是佔用內存較小,以及性能較優(http://www.javashuo.com/article/p-dgffmcio-z.htmlhtml

JexuxNginx性能對比參考(https://www.aliyun.com/jiaocheng/202277.html linux

 


 

直入正題。。。。。。。。。。nginx

Nginx 安裝 c++

安裝所需環境 正則表達式

Nginx 是 C語言 開發,建議在 Linux 上運行,固然,也能夠安裝 Windows 版本,本篇則使用 CentOS 7 做爲安裝環境。算法

 

. gcc 安裝 vim

安裝 nginx 須要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gcc 環境,則須要安裝:後端

yum install gcc-c++api

 

. PCRE pcre-devel 安裝 瀏覽器

PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,因此須要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也須要此庫。命令:

yum install -y pcre pcre-devel

 

. zlib 安裝

zlib 庫提供了不少種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,因此須要在 Centos 上安裝 zlib 庫。

yum install -y zlib zlib-devel

 

. OpenSSL 安裝

OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。

nginx 不只支持 http 協議,還支持 https(即在ssl協議上傳輸http),因此須要在 Centos 安裝 OpenSSL 庫。

yum install -y openssl openssl-devel

 

官網下載

1.直接下載.tar.gz安裝包,地址:https://nginx.org/en/download.html

 

2.使用wget命令下載(推薦)。

wget -c https://nginx.org/download/nginx-1.14.0.tar.gz

 

我下載的是1.14.0版本,這個是目前的穩定版。

解壓

依然是直接命令:

tar -zxvf nginx-1.14.0.tar.gz cd nginx-1.14.0 

其實在 nginx-1.10.1 版本中你就不須要去配置相關東西,默認就能夠了。固然,若是你要本身配置目錄也是能夠的。

1.使用默認配置

./configure

 

編譯安裝

make make install

查找安裝路徑:

whereis nginx

 

啓動、中止nginx

cd /usr/local/nginx/sbin/ ./nginx ./nginx -s stop ./nginx -s quit ./nginx -s reload

./nginx -s quit:此方式中止步驟是待nginx進程處理任務完畢進行中止。

./nginx -s stop:此方式至關於先查出nginx進程id再使用kill命令強制殺掉進程。

查詢nginx進程:

ps aux|grep nginx

 

重啓 nginx

1.先中止再啓動(推薦):

nginx 進行重啓至關於先中止再啓動,即先執行中止命令再執行啓動命令。以下:

./nginx -s quit ./nginx

2.從新加載配置文件:

ngin x的配置文件 nginx.conf 修改後,要想讓配置生效須要重啓 nginx,使用-s reload不用先中止 ngin x再啓動 nginx 便可將配置信息在 nginx 中生效,以下:

./nginx -s reload

啓動成功後,在瀏覽器能夠看到這樣的頁面:

添加到系統服務,開機自啓動

vim /etc/init.d/nginx

添加內容以下

#!/bin/bash

#chkconfig:- 99 20

#description:Nginx Service Control Script

cmd='/usr/local/nginx/sbin/nginx'

pid='/usr/local/nginx/logs/nginx.pid'

case "$1" in

start)

    $cmd

    ;;

stop)

    kill -s QUIT $(cat $pid)

    ;;

restart)

    $0 stop

    $0 start

    ;;

reload)

    kill -s HUP  $(cat $pid)

    ;;

*)

    echo 'Usage:$0 {start|stop|restart|reload}'

    exit 1

esac

exit 0

 

重啓服務

service nginx restart

 

到這裏nginx安裝完成了

 

站點配置 

HTTP配置代理  

若是在 nginx.conf 文件中添加站點配置,多站點的狀況下,不利於管理 

多站點配置,須要啓用這個配置,而後在conf.d文件夾下,建立多個配置文件便可。好比lotteryAdmin.conflotteryAPI.conf

 新建文件夾

mkdir /usr/local/nginx/conf.d/ 

vim /usr/local/nginx/conf/nginx.conf

在尾部 http { }內添加 

include /usr/local/nginx/conf.d/*.conf; 

而後 :wq!保存

 

 

 

vim  /usr/local/nginx/conf.d/lotteryAdmin.conf

添加內容

 server {

        listen 80; 

        index /Main/index; 

        server_name admin1.lottery.com; #域名 

        location / {

        # 傳遞真實IP到後端

        proxy_set_header Host $http_host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass  http://localhost:5001;#dotNetCore程序的端口號

        }

    }

 

而後 :wq!保存

 

重啓 Nginx 服務

service nginx restart

查看Nginx配置是否正常

 /usr/local/nginx/sbin/nginx -t

 

如今能夠訪問你的站點了

HTTPS配置代理

申請到證書以後,我這裏有 xxx.keyxxx.pem兩個文件

server {

    listen 443;

    server_name api.lottery.com;

    ssl on;

    index index.html index.htm;

    ssl_certificate  /dotnet/httpsKey/cretc.pem;

    ssl_certificate_key  /dotnet/httpsKey/cretc.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_set_header Host $http_host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass  https://localhost:5003;#dotNetCore程序的端口號

    }

}

 

wq!保存

service nginx restart

若是報錯

unknown directive "ssl"............

 

切換到你解壓的nginx目錄下

####### 下載你當前版本的nginx包,而且解壓 進到目錄

./configure --with-http_ssl_module

####### 切記千萬不要make install 那樣就覆蓋安裝了

make

####### 將原來的nginx備份 備份以前先kill當前正在啓動的nginx

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

####### make以後會在當前目錄生成 objs 目錄

cp objs/nginx /usr/local/nginx/sbin/nginx

####### 而後從新啓動nginx

/usr/local/nginx/sbin/nginx

 

在重啓nginx服務

service nginx restart

這個時候能夠訪問你的站點了

相關文章
相關標籤/搜索