Centos7安裝nginx(源碼安裝 不是yum)和安裝Stream模塊的編譯


nginx從 1.9.0開始,新增加了一個stream模塊,用來實現四層協議的轉發、代理或者負載均衡等。這完全就是搶HAproxy份額的節奏,鑒於nginx在7層負載均衡和web service上的成功,和nginx良好的框架,stream模塊前景一片光明。
ngx_stream_core_module模塊
是模擬反代基於tcp或udp的服務連接,即工作於傳輸層的反代或調度器

Nginx版本:1.18.0

1.下載NGINX穩定發行版

wget https://nginx.org/download/nginx-1.18.0.tar.gz

2.解壓並切換到安裝目錄

tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

3.編譯安裝

默認路徑安裝

yum -y install gcc gcc-c++ autoconf automake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

./configure --with-stream或者

./configure --with-stream --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-pcre --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_stub_status_module --with-stream



make
&& make install


配置文件路徑為/usr/local/nginx/conf/ 
和啟動路徑/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx  -s start

 

 

指定nginx.config路徑安裝

yum -y install gcc gcc-c++ autoconf automake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

./configure --prefix=/opt/nginx --sbin-path=/opt/nginx/sbin/nginx --conf-path=/opt/nginx/conf/nginx.conf --with-http_stub_status_module --with-http_gzip_static_module --with-stream
make
make install
cd /opt/nginx
配置文件路徑/opt/nginx/conf/nginx.conf
和啟動路徑/opt/nginx/sbin/nginx -s reload

yum卸載nginx參考鏈接 https://blog.csdn.net/a704397849/article/details/100569176

源碼安裝卸載 sudo rm -f -R /usr/local/nginx && rm -f /usr/local/sbin/nginx

nginx:未找到命令(command not found)解決方法 加入環境變量:https://www.jianshu.com/p/3aef06a614bc

service nginx start啟動nginx出現Failed to start nginx.service:unit not found   :https://blog.csdn.net/qq_38269316/article/details/82860941

 

nginx基本操作命令

    systemctl start nginx.service(service nginx start)

    systemctl stop nginx.service(service nginx stop)

    systemctl reload nginx.service(service nginx restart)

    systemctl status nginx.service(service nginx status)

補充資料

--prefix=path:設置Nginx的安裝路徑,不寫的話默認是在/usr/local/nginx
--sbin-path=path:設置Nginx的可執行文件路徑,默認路徑是prefix/sbin/nginx
--conf-path=path:設置Nginx配置文件路徑,默認路徑是prefix/conf/nginx.conf
--pid-path=path:設置Nginx pid文件路徑,默認路徑是prefix/logs/nginx.pid
--error-log-path=path:設置錯誤日志存放路徑,默認路徑是prefix/logs/error.log
--http-log-path=path:設置訪問日志存放路徑,默認路徑是prefix/logs/access.log
--user=name:設置運行Nginx的用戶,默認用戶是nobody
--group=name:設置運行Nginx的用戶組,默認用戶組是nobody
--with-http_ssl_module:啟用Nginx的SSL功能
--with-http_realip_module:該模塊可以記錄原始客戶端的IP而不是負載均衡的IP
--with-http_sub_module:文字內容替換模塊,可用於替換全站敏感字等
--with-http_flv_module:開啟對FLV格式文件的支持
--with-http_mp4_module:開啟對MP4格式文件的支持
--with-http_gzip_module:提供對gzip壓縮的支持
--with-http_stub_status_module:開啟Nginx狀態監控模塊
--with-pcre:支持正則表達式

Nginx主配置文件參數詳解

user nginx; #定義運行Nginx的用戶
worker_processes 2;#Nginx所開啟的進程數,通常和cpu個數相等或者設置為auto
worker_cpu_affinity auto; #自動進行CPU親和設置
worker_cpu_affinity 0000000000000001 000000000000010 #手動進行CPU親和設置
worker_rlimit_nofile 65535; #一個worker進程最多能打開的文件數
error_log logs/error.log warn; #Nginx服務的錯誤日志路徑與記錄級別
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535; #設置Nginx進程文件句柄數
events { worker_connections 10240; #每個進程的並發數 use epoll; }
http {
include mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型
charset utf-8; #默認字符集
log_format main #定義日志格式
access_log logs/access.log main; #訪問日志存放路徑與日志記錄格式,這里main就是上一步log_format所定義的main
sendfile on;
tcp_nopush on; #一次傳輸多個數據包,提高傳輸效率
#tcp_nodeley off #與tcp_nopush相反,實時性要求比較高的場景會打開這個
keepalive_timeout 65; #長連接超時時間為65秒
gzip on; #打開gzip后通過瀏覽器開發者工具-網絡功能可以看到size大小被壓縮了,對文本類型的文件壓縮效率最高,可作用於location中
include /etc/nginx/conf.d/*.conf #conf.d目錄下的配置文件也會生效
server { listen 80; server_name linuxe.cn www.linuxe.cn; #精確匹配優先級最高,支持正則匹配,如果都不匹配則匹配default server access_log logs/access.log main; #單獨對主機記錄日志 location ~ .*\.(jpg|gif|png)$ { gzip on; expires 24h; #開啟緩存,如果是取的緩存數據,瀏覽器開發者工具中返回狀態是304 root html; index index.html index.htm; } #error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
其中main和events都是全局性標簽;
而server標簽負責了每個虛擬主機的配置,
在server標簽中還存在localtion標簽,可以根據條件匹配來對不同的訪問路徑做不同的配置,實現URL轉發等功能。
localtion標簽有着一個匹配順序需要注意,這是經常出錯的地方,下面是localtion優先級排序示例;
location = /uri #精確匹配,優先級最高
location ^~ /uri #普通字符串匹配,不支持正則表達式,當匹配成功后停止其他location匹配,優先級高於正則
location ~ #區分大小寫的正則匹配
location ~* #不區分大小寫的正則匹配
location /uri #前綴匹配
location / #通用匹配

 

sudo rm -f -R /usr/local/nginx && rm -f /usr/local/sbin/nginx


重新安裝http://blog.chinaunix.net/uid-69994259-id-5845151.html


已經yum安裝的 需要添加第三方模塊或者平滑升級時候 參考下面鏈接
https://www.136.la/nginx/show-77740.html
https://blog.csdn.net/qq_37638061/article/details/90581358
RPM指定版本安裝+安裝第三方模塊
https://www.cnblogs.com/sky-cheng/p/14621145.html
https://blog.csdn.net/weixin_30965253/article/details/116893322



免責聲明!

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



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