ubuntu環境下nginx的編譯安裝以及相關設定,ubuntunginx

來源:互聯網
上載者:User

ubuntu環境下nginx的編譯安裝以及相關設定,ubuntunginx

一、基本的編譯與安裝

1、安裝依賴項

sudo apt-get updatesudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libxml2-dev libgd2-xpm-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libtoolsudo apt-get install openssl

2、下載新版本,到官網複製下載連結

wget http://nginx.org/download/nginx-1.13.5.tar.gz

3、解壓

tar -zxvf nginx-1.13.5.tar.gz

4、編譯安裝

# 進入解壓目錄:cd nginx-1.13.5# 配置:這裡額外安裝幾個模組./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-mail=dynamic# 編輯nginx:sudo make# 安裝nginx:sudo make install# 啟動nginx:sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf#注意:-c 指定設定檔的路徑,不加的話,nginx會自動載入預設路徑的設定檔,可以通過 -h查看協助命令。# 查看nginx進程:ps -ef|grep nginx# 建立軟連結(由於/usr/local/bin包含於$PATH當中,這樣就不需要額外的設定環境變數了,這意味著可以在其他路徑下直接運行nginx命令)sudo ln -s /opt/dotnet/dotnet /usr/local/bin #(建立連結)

 編譯選項說明:

--prefix=path 如果在編譯的不指定安裝位置,那麼預設的位置/usr/local/nginx目錄
--sbin-path=path 設定nginx執行指令碼的位置,這裡如果設定在path變數裡面,就可以在bash環境下,任意使用nginx命令,預設位置prefix/sbin/nginx  注意這裡的prefix是在設定檔裡面配置的路徑
--conf-path=path 配置nginx設定檔的路徑,如果不指定這個選項,那麼設定檔的預設路徑就會是 prefix/conf/nginx.conf
--pid-path =path 配置nginx.pid file的路徑,一般來說,進程在啟動並執行時候的時候有一個進程id,這個id會儲存在pid file裡面,預設的pid file的置放位置是prefix/logs/nginx.pid
--error-log-path=path 設定錯誤記錄檔的存放路徑,如果不指定,就預設 prefix/logs/error.log
--http-log-path= path 設定http訪問日誌的路徑,如果不指定,就預設 prefix/logs/access.log
--user=name 設定預設啟動進程的使用者,如果不指定,就預設 nobody
--group=name 設定這個使用者所在的使用者組,如果不指定,依然是nobody
--with-http_ssl_module 開啟HTTP SSL模組,使NGINX可以支援HTTPS請求。需要安裝了OPENSSL  
--with-http_flv_module  
--with-http_stub_status_module 啟用 "server status" 頁
--without-http_gzip_module 禁用 ngx_http_gzip_module. 如果啟用,需要 zlib
--without-http_ssi_module 禁用 ngx_http_ssi_module  
--without-http_referer_module 禁用 ngx_http_referer_module  
--without-http_rewrite_module 禁用 ngx_http_rewrite_module. 如果啟用需要 PCRE 。  
--without-http_proxy_module 禁用 ngx_http_proxy_module  
--without-http_fastcgi_module 禁用 ngx_http_fastcgi_module  
--without-http_memcached_module 禁用 ngx_http_memcached_module  
--without-http_browser_module 禁用 ngx_http_browser_module 
--http-proxy-temp-path=PATH 設定路徑到the http proxy temporary files  
--http-fastcgi-temp-path=PATH 設定路徑到Set path to the http fastcgi temporary files  
--without-http 禁用 HTTP server
--with-mail 啟用 IMAP4/POP3/SMTP 代理模組  
--with-mail_ssl_module 啟用ngx_mail_ssl_module  
--with-openssl=DIR 設定路徑到OpenSSL library sources  
--with-stream 用來實現四層協議的轉寄、代理或者負載平衡等

二、ssl的相關配置

1、使用自己產生的認證(僅作測試或加密資料用,不被主流瀏覽器支援) 

# 建立伺服器私密金鑰(過程需要輸入密碼,請記住這個密碼)產生RSA密鑰sudo openssl genrsa -des3 -out testcert.key 1024  # 產生一個認證請求   # 需要依次輸入國家,地區,組織,email。# 最重要的是有一個common name,可以寫你的名字或者網域名稱。如果為了https申請(不使用自己產生的認證,向CA申請),這個必須和網域名稱吻合,否則會引發瀏覽器警報。產生的csr檔案交給CA簽名後形成服務端自己的認證sudo openssl req -new -key testcert.key -out testcert.csr# 產生不要求輸入密碼的keysudo openssl rsa -in testcert.key -out testcert_nopwd.key  # 產生crt檔案sudo openssl x509 -req -days 365 -in testcert.csr -signkey testcert_nopwd.key -out testcert.crt  

之後將testcert_nopwd.key、testcert.csr、testcert.crt檔案複製到 /etc/ssl/certs 目錄下

然後配置 nginx.conf 檔案

由於在編譯中設定的路徑是/usr/local/nginx,因此該檔案位於/usr/local/nginx/conf 目錄下。
開啟改檔案,在http模組作如下設定:
ssl_certificate /etc/ssl/certs/testcert.crt;
ssl_certificate_key /etc/ssl/certs/testcert.key;

 具體的代碼:

http {    include       proxy.conf;    include       mime.types;    default_type  application/octet-stream;      limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;    server_tokens off;        sendfile        on;    #tcp_nopush     on;       keepalive_timeout  29;    client_body_timeout 10;     client_header_timeout 10;     send_timeout 10;    #gzip  on;    # mvctest是一個名稱,與具體的程式相關    upstream mvctest{        server localhost:5000;    }    server {               listen       80;        add_header Strict-Transport-Security max-age=15768000;        return 301 https://$host$request_uri;    }    server {        listen *:443    ssl;        server_name     localhost;        ssl_certificate /etc/ssl/certs/testcert.crt;        ssl_certificate_key /etc/ssl/certs/testcert.key;        ssl_protocols TLSv1.1 TLSv1.2;        ssl_prefer_server_ciphers on;        ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";        ssl_ecdh_curve secp384r1;        ssl_session_cache shared:SSL:10m;        ssl_session_tickets off;        ssl_stapling on; #ensure your cert is capable        ssl_stapling_verify on; #ensure your cert is capable        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";        add_header X-Frame-Options SAMEORIGIN;        add_header X-Content-Type-Options nosniff;        #Redirects all traffic        location / {            proxy_pass  http://localhost:5000;            limit_req   zone=one burst=10 nodelay;            limit_req_status 503;        }    }}

上面定義的地區名稱為one(zone=one),分配記憶體大小為10m(如果限制域的儲存空間耗盡了,對於後續所有請求,伺服器都會返回 503),
同一個ip($binary_remote_addr)平均處理的請求頻率不能超過每秒5次(rate=5r/s); 如果超過每秒5次但超過的請求數量小於等於10(burst=10)時,會延遲請求。
如果超過每秒5次的請求數超過10,則立即返回503(limit_req_status 503)給用戶端

2、申請認證
常用免費認證:https://letsencrypt.org/
通常來說,將CSR檔案(產生方式同上,或者按照CA的要求)就是你需要提交給 SSL 認證機構的,當你的網域名稱或組織通過驗證後,認證機構就會頒發給你兩個檔案:test_com.crt和test_com.ca-bundle
而test_com.key是需要用在 Nginx 配置裡和test_com.crt、test_com.ca-bundle配合使用的,需要好好保管。

#使用cat命令產生cat test_com.crt  test_com.ca-bundle > test_com.pem

最終得到兩個檔案:test_com.key和test_com.pem,為了統一位置,可以把這二個檔案都移動到 /etc/ssl/private 目錄或者 /etc/ssl/cert 目錄。
然後可以修改nginx.conf檔案,如下:

server {      #...    ssl on;    ssl_certificate /etc/ssl/private/test_com.pem;    ssl_certificate_key /etc/ssl/private/test_com.key;    #...}

同使用自產生認證基本一樣,只是重新替換為認證機構頒發的認證而已。

3、迪菲-赫爾曼金鑰交換
以上這麼做並不安全,預設是 SHA-1 形式,而現在主流的方案應該都避免 SHA-1,為了確保更強的安全性,我們可以採取迪菲-赫爾曼金鑰交換
首先,進入/etc/ssl/certs目錄,執行命令 sudo  openssl dhparam -out dhparam.pem 4096 產生一個dhparam.pem 

test@test-VirtualBox:/etc/ssl/certs$ sudo  openssl dhparam -out dhparam.pem 4096[sudo] test 的密碼:Generating DH parameters, 4096 bit long safe prime, generator 2This is going to take a long time# ...會等一段時間,與機器效能有關...#完成後可以查看產生的檔案test@test-VirtualBox:~$ cd /etc/ssl/certs/test@test-VirtualBox:/etc/ssl/certs$ ll dhparam.pem -rw-r--r-- 1 root root 769 9月  27 11:49 dhparam.pem

修改 nginx.conf 檔案,添加 ssl_dhparam /etc/ssl/certs/dhparam.pem; 即可

ssl_prefer_server_ciphers on;ssl_dhparam /etc/ssl/certs/dhparam.pem;ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";

 

三、啟動相關

1、常用命令

#啟動 Nginx/usr/local/nginx/sbin/nginx ./sbin/nginx #停止 Nginx./sbin/nginx -s stop ./sbin/nginx -s quit# -s都是採用向 Nginx 發送訊號的方式。#Nginx重新載入配置./sbin/nginx -s reload#指定設定檔./sbin/nginx -c /usr/local/nginx/conf/nginx.conf#-c表示configuration,指定設定檔#查看 Nginx 版本./sbin/nginx -v#協助./sbin/nginx -h./sbin/nginx -?

2、自動啟動
編譯安裝需要自己進行設定方可自動啟動

# 設定nginx自啟動,在/lib/systemd/system/ 目錄下建立一個服務檔案vim /lib/systemd/system/nginx.service

內容如下:

[Unit]Description=nginx - high performance web serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stop[Install]WantedBy=multi-user.target

檔案說明
[Unit]部分
Description:描述服務
After:依賴,當依賴的服務啟動之後再啟動自訂的服務

[Service]部分
Type=forking是後台啟動並執行形式
ExecStart為服務的具體運行命令(需要根據路徑適配)
ExecReload為重啟命令(需要根據路徑適配)
ExecStop為停止命令(需要根據路徑適配)
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:啟動、重啟、停止命令全部要求使用絕對路徑

[Install]部分
服務安裝的相關設定,可設定為多使用者

# 設定了自啟動後,任意目錄下執行systemctl enable nginx.service# 啟動nginx服務systemctl start nginx.service# 設定開機自動啟動systemctl enable nginx.service# 停止開機自動啟動systemctl disable nginx.service# 查看狀態systemctl status nginx.service# 重啟服務systemctl restart nginx.service# 查看所有服務systemctl list-units --type=service

四、可能遇到的問題
nginx: [emerg] SSL_CTX_use_PrivateKey_file("/etc/ssl/certs/testcert.key") failed (SSL: error:0906406D:PEM routines:....
nginx啟動的時候需要輸入認證密碼,解決辦法是可以使用私密金鑰來產生解密後的key來代替,效果是一樣的(就跟ssh串連差不多),達到免密碼重啟的效果:
openssl rsa -in testcert.key -out untestcert.key
接下來你只需要在nginx裡面使用新的untestcert.key即可使用https串連了。

nginx: [emerg] zero size shared memory zone "oneip"
出現此錯誤都是因為在未指定limit_req_zone指定就使用了limit_req指令的原因(或者limit_req中的name值和limit_req_zone中定義的name值不一致)。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.