日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Ubuntu下編譯安裝Nginx及相關(guān)設(shè)置

ubuntu下編譯安裝Nginx及相關(guān)設(shè)置

一、基本的編譯與安裝

1、安裝依賴項(xiàng)

sudo apt-get update
sudo 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 libtool
sudo apt-get install openssl

2、下載新版本,到官網(wǎng)復(fù)制下載鏈接

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

3、解壓

tar -zxvf nginx-1.13.5.tar.gz

4、編譯安裝

# 進(jìn)入解壓目錄:
cd nginx-1.13.5
# 配置:這里額外安裝幾個(gè)模塊
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-mail=dynamic

# 編輯nginx:
sudo make
# 安裝nginx:
sudo make install
# 啟動(dòng)nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#注意:-c 指定配置文件的路徑,不加的話,nginx會(huì)自動(dòng)加載默認(rèn)路徑的配置文件,可以通過 -h查看幫助命令。

# 查看nginx進(jìn)程:
ps -ef|grep nginx

# 建立軟鏈接(由于/usr/local/bin包含于$PATH當(dāng)中,這樣就不需要額外的設(shè)置環(huán)境變量了,這意味著可以在其他路徑下直接運(yùn)行nginx命令)
sudo ln -s /opt/dotnet/dotnet /usr/local/bin #(創(chuàng)建鏈接)

 編譯選項(xiàng)說明:

--prefix=path 如果在編譯的不指定安裝位置,那么默認(rèn)的位置/usr/local/nginx目錄
--sbin-path=path 設(shè)置nginx執(zhí)行腳本的位置,這里如果設(shè)置在path變量里面,就可以在bash環(huán)境下,任意使用nginx命令,默認(rèn)位置prefix/sbin/nginx  注意這里的prefix是在配置文件里面配置的路徑
--conf-path=path 配置nginx配置文件的路徑,如果不指定這個(gè)選項(xiàng),那么配置文件的默認(rèn)路徑就會(huì)是 prefix/conf/nginx.conf
--pid-path =path 配置nginx.pid file的路徑,一般來說,進(jìn)程在運(yùn)行的時(shí)候的時(shí)候有一個(gè)進(jìn)程id,這個(gè)id會(huì)保存在pid file里面,默認(rèn)的pid file的放置位置是prefix/logs/nginx.pid
--error-log-path=path 設(shè)置錯(cuò)誤日志的存放路徑,如果不指定,就默認(rèn) prefix/logs/error.log
--http-log-path= path 設(shè)置http訪問日志的路徑,如果不指定,就默認(rèn) prefix/logs/access.log
--user=name 設(shè)置默認(rèn)啟動(dòng)進(jìn)程的用戶,如果不指定,就默認(rèn) nobody
--group=name 設(shè)置這個(gè)用戶所在的用戶組,如果不指定,依然是nobody
--with-http_ssl_module 開啟HTTP SSL模塊,使NGINX可以支持HTTPS請(qǐng)求。需要安裝了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 設(shè)置路徑到the http proxy temporary files 
--http-fastcgi-temp-path=PATH 設(shè)置路徑到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 設(shè)置路徑到OpenSSL library sources 
--with-stream 用來實(shí)現(xiàn)四層協(xié)議的轉(zhuǎn)發(fā)、代理或者負(fù)載均衡等

二、ssl的相關(guān)配置

1、使用自己生成的證書(僅作測試或加密數(shù)據(jù)用,不被主流瀏覽器支持) 

# 建立服務(wù)器私鑰(過程需要輸入密碼,請(qǐng)記住這個(gè)密碼)生成RSA密鑰
sudo openssl genrsa -des3 -out testcert.key 1024  
# 生成一個(gè)證書請(qǐng)求   
# 需要依次輸入國家,地區(qū),組織,email。
# 最重要的是有一個(gè)common name,可以寫你的名字或者域名。如果為了https申請(qǐng)(不使用自己生成的證書,向CA申請(qǐng)),這個(gè)必須和域名吻合,否則會(huì)引發(fā)瀏覽器警報(bào)。生成的csr文件交給CA簽名后形成服務(wù)端自己的證書
sudo openssl req -new -key testcert.key -out testcert.csr
# 生成不需要密碼的key
sudo 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文件復(fù)制到 /etc/ssl/certs 目錄下

然后配置 nginx.conf 文件

由于在編譯中設(shè)置的路徑是/usr/local/nginx,因此該文件位于/usr/local/nginx/conf 目錄下。
打開改文件,在http模塊作如下設(shè)置:
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是一個(gè)名稱,與具體的程序相關(guān)
    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;
        }
    }

}

上面定義的區(qū)域名稱為one(zone=one),分配內(nèi)存大小為10m(如果限制域的存儲(chǔ)空間耗盡了,對(duì)于后續(xù)所有請(qǐng)求,服務(wù)器都會(huì)返回 503),
同一個(gè)ip($binary_remote_addr)平均處理的請(qǐng)求頻率不能超過每秒5次(rate=5r/s); 如果超過每秒5次但超過的請(qǐng)求數(shù)量小于等于10(burst=10)時(shí),會(huì)延遲請(qǐng)求。
如果超過每秒5次的請(qǐng)求數(shù)超過10,則立即返回503(limit_req_status 503)給客戶端

2、申請(qǐng)證書
常用免費(fèi)證書:https://letsencrypt.org/
通常來說,將CSR文件(生成方式同上,或者按照CA的要求)就是你需要提交給 SSL 認(rèn)證機(jī)構(gòu)的,當(dāng)你的域名或組織通過驗(yàn)證后,認(rèn)證機(jī)構(gòu)就會(huì)頒發(fā)給你兩個(gè)文件: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

最終得到兩個(gè)文件:test_com.key和test_com.pem,為了統(tǒng)一位置,可以把這二個(gè)文件都移動(dòng)到 /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;     #...

}

同使用自生成證書基本一樣,只是重新替換為認(rèn)證機(jī)構(gòu)頒發(fā)的證書而已。

3、迪菲-赫爾曼密鑰交換
以上這么做并不安全,默認(rèn)是 SHA-1 形式,而現(xiàn)在主流的方案應(yīng)該都避免 SHA-1,為了確保更強(qiáng)的安全性,我們可以采取迪菲-赫爾曼密鑰交換
首先,進(jìn)入/etc/ssl/certs目錄,執(zhí)行命令 sudo  openssl dhparam -out dhparam.pem 4096 生成一個(gè)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 2
This is going to take a long time
# ...會(huì)等一段時(shí)間,與機(jī)器性能有關(guān)...
#完成后可以查看生成的文件
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";

三、啟動(dòng)相關(guān)

1、常用命令

#啟動(dòng) Nginx
/usr/local/nginx/sbin/nginx 
./sbin/nginx 
#停止 Nginx
./sbin/nginx -s stop 
./sbin/nginx -s quit
# -s都是采用向 Nginx 發(fā)送信號(hào)的方式。
#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、自動(dòng)啟動(dòng)
編譯安裝需要自己進(jìn)行設(shè)置方可自動(dòng)啟動(dòng)

# 設(shè)置nginx自啟動(dòng),在/lib/systemd/system/ 目錄下創(chuàng)建一個(gè)服務(wù)文件
vim /lib/systemd/system/nginx.service

內(nèi)容如下:

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

文件說明
[Unit]部分
Description:描述服務(wù)
After:依賴,當(dāng)依賴的服務(wù)啟動(dòng)之后再啟動(dòng)自定義的服務(wù)

[Service]部分
Type=forking是后臺(tái)運(yùn)行的形式
ExecStart為服務(wù)的具體運(yùn)行命令(需要根據(jù)路徑適配)
ExecReload為重啟命令(需要根據(jù)路徑適配)
ExecStop為停止命令(需要根據(jù)路徑適配)
PrivateTmp=True表示給服務(wù)分配獨(dú)立的臨時(shí)空間
注意:啟動(dòng)、重啟、停止命令全部要求使用絕對(duì)路徑

[Install]部分
服務(wù)安裝的相關(guān)設(shè)置,可設(shè)置為多用戶

# 設(shè)置了自啟動(dòng)后,任意目錄下執(zhí)行
systemctl enable nginx.service
# 啟動(dòng)nginx服務(wù)
systemctl start nginx.service
# 設(shè)置開機(jī)自動(dòng)啟動(dòng)
systemctl enable nginx.service
# 停止開機(jī)自動(dòng)啟動(dòng)
systemctl disable nginx.service
# 查看狀態(tài)
systemctl status nginx.service
# 重啟服務(wù)
systemctl restart nginx.service
# 查看所有服務(wù)
systemctl list-units --type=service

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

nginx: [emerg] zero size shared memory zone "oneip"
出現(xiàn)此錯(cuò)誤都是因?yàn)樵谖粗付╨imit_req_zone指定就使用了limit_req指令的原因(或者limit_req中的name值和limit_req_zone中定義的name值不一致)。


標(biāo)題名稱:Ubuntu下編譯安裝Nginx及相關(guān)設(shè)置
標(biāo)題路徑:http://m.5511xx.com/article/cohhjjj.html