nginx

安裝步驟

一、 gcc 安裝

安裝 nginx 需要先將官網(wǎng)下載的源碼進(jìn)行編譯,編譯依賴 gcc 環(huán)境,如果沒有 gcc 環(huán)境,則需要安裝:

yum install gcc-c++

二. PCRE pcre-devel 安裝

PCRE(Perl Compatible Regular Expressions) 是一個(gè)Perl庫(kù),包括 perl 兼容的正則表達(dá)式庫(kù)。nginx 的 http 模塊使用 pcre 來解析正則表達(dá)式,所以需要在 linux 上安裝 pcre 庫(kù),pcre-devel 是使用 pcre 開發(fā)的一個(gè)二次開發(fā)庫(kù)。nginx也需要此庫(kù)。命令:

yum install -y pcre pcre-devel

三. zlib 安裝

zlib 庫(kù)提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對(duì) http 包的內(nèi)容進(jìn)行 gzip ,所以需要在 Centos 上安裝 zlib 庫(kù)。

yum install -y zlib zlib-devel

四. OpenSSL 安裝

OpenSSL 是一個(gè)強(qiáng)大的安全套接字層密碼庫(kù),囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協(xié)議,并提供豐富的應(yīng)用程序供測(cè)試或其它目的使用。
nginx 不僅支持 http 協(xié)議,還支持 https(即在ssl協(xié)議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫(kù)。

yum install -y openssl openssl-devel

五、nginx安裝

下載
wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
解壓
tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1
配置
./configure
編譯安裝
make
make install

查找安裝路徑 whereis nginx

啟動(dòng)、停止nginx
cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop 此方式相當(dāng)于先查出nginx進(jìn)程id再使用kill命令強(qiáng)制殺掉進(jìn)程。
./nginx -s quit 此方式停止步驟是待nginx進(jìn)程處理任務(wù)完畢進(jìn)行停止。
./nginx -s reload

檢測(cè)nginx.conf配置文件是否正確:./nginx -t
查詢nginx進(jìn)程 ps aux|grep nginx

重啟 nginx

1.先停止再啟動(dòng):

./nginx -s quit
./nginx

2.重新加載配置文件:

./nginx -s reload
開機(jī)自啟動(dòng)

即在rc.local增加啟動(dòng)代碼就可以了。

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx
設(shè)置執(zhí)行權(quán)限:

chmod 755 rc.local
配置Nginx的SSL模塊

當(dāng) Nginx如果未開啟SSL模塊,配置Https時(shí)提示錯(cuò)誤,如nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37。則需要Nginx開啟SSL模塊
切換到源碼包:

cd /home/test/nginx

查看nginx原有的模塊

/usr/local/nginx/sbin/nginx -V

在configure arguments:后面顯示的原有的configure參數(shù)如下:

--prefix=/usr/local/nginx --with-http_stub_status_module

那么我們的新配置信息就應(yīng)該這樣寫:

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

運(yùn)行上面的命令即可,等配置完
配置完成后,運(yùn)行命令

make

這里不要進(jìn)行make install,否則就是覆蓋安裝
然后備份原有已安裝好的nginx

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

然后將剛剛編譯好的nginx覆蓋掉原有的nginx(這個(gè)時(shí)候nginx要停止?fàn)顟B(tài))

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

然后啟動(dòng)nginx,仍可以通過命令查看是否已經(jīng)加入成功

/usr/local/nginx/sbin/nginx -V
Nginx 配置Http和Https共存
server {
            listen 80 default backlog=2048;
            listen 443 ssl;
            server_name wosign.com;
            root /var/www/html;
            ssl_certificate /usr/local/Tengine/sslcrt/ wosign.com.crt;
            ssl_certificate_key /usr/local/Tengine/sslcrt/ wosign.com .Key;
        }

把ssl on;這行去掉,ssl寫在443端口后面。這樣http和https的鏈接都可以用

Nginx 配置SSL安全證書重啟避免輸入密碼

可以用私鑰來做這件事。生成一個(gè)解密的key文件,替代原來key文件。

openssl rsa -in server.key -out server.key.unsecure
Nginx SSL性能調(diào)優(yōu)
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
反向代理
  location / {
            proxy_pass  http://localhost:8080;
            proxy_set_header    Host    $host;
            proxy_set_header    X-Real-IP   $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
全站加密
完整版nginx.conf配置文件
#user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

 access_log  /home/summer/logs/web-admin.access.log;
    error_log  /home/summer/logs/web-admin.error.log;  

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    server {
        listen 443;
        server_name www.realeshoes.com;
        ssl on;
            # 配置根目錄
        root   /home/summer/reale/web-admin;
    
        ssl_certificate 1_www.realeshoes.com_bundle.crt;
        ssl_certificate_key 2_www.realeshoes.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
# 配置靜態(tài)目錄
 location /admin {   
        alias   /home/summer/reale/web-admin;
        index  index.html;
    }
        location /api/ {
            proxy_pass  http://localhost:8080/;
            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.realeshoes.com;
        rewrite https://$server_name$request_uri? permanent;
    }

    #include /etc/nginx/conf.d/*.conf;
    #include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容