nginx編譯安裝及常用參數(shù)詳解
1 基于ansible role實(shí)現(xiàn)編譯安裝nginx
利用ansible控制端10.0.0.8機(jī)器,在被控制端10.0.0.18上部署nginx
首先打通ansible控制端與被控制端的基于key驗(yàn)證
[root@ansible-rocky ~]$ ssh-copy-id 10.0.0.18 [root@ansible-rocky ~]$ ssh 10.0.0.18 Last login: Wed Jan 11 12:18:28 2023 from 10.0.0.8 [root@rocky8 ~]$ hostname -I 10.0.0.18
然后創(chuàng)建nginx項(xiàng)目目錄實(shí)現(xiàn)基于role的部署任務(wù)
#nginx role項(xiàng)目目錄總覽 [root@ansible-rocky opt]$ tree /opt /opt ├── hosts_nginx ├── nginx_role.yml └── roles └── nginx ├── handlers │ └── main.yml ├── tasks │ └── main.yml └── templates ├── nginx.conf.j2 └── nginx.service.j2 #task文件 [root@ansible-rocky roles]$ cat nginx/tasks/main.yml - name: add group nginx group: name=nginx system=yes gid=80 - name: add user nginx user: name=nginx group=nginx uid=80 system=yes shell="/sbin/nologin" create_home=no - name: install dependent package yum: name={{item}} state=latest loop: - gcc - make - pcre-devel - openssl-devel - zlib-devel - perl-ExtUtils-Embed - name: get nginx source unarchive: src: "{{ url }}" dest: "/usr/local/src" remote_src: yes - name: compile and install shell: cmd: "./configure --prefix={{install_dir}} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install" chdir: "/usr/local/src/nginx-{{ version }}" creates: "{{install_dir}}/sbin/nginx" - name: config file template: src: nginx.conf.j2 dest: "{{install_dir}}/conf/nginx.conf" owner: nginx group: nginx notify: restart service tags: - config - name: create directory file: path: "{{install_dir}}/conf/conf.d" state: directory owner: nginx group: nginx - name: change install directory owner file: path: "{{install_dir}}" owner: nginx group: nginx recurse: yes - name: copy service file template: src: nginx.service.j2 dest: "/lib/systemd/system/nginx.service" - name: check config shell: cmd: "{{install_dir}}/sbin/nginx -t" register: check_nginx_config changed_when: - check_nginx_config.stdout.find('successful') - false - name: start service systemd: daemon_reload: yes name: nginx.service state: started enabled: yes #創(chuàng)建handler文件 [root@ansible-rocky roles]$ cat nginx/handlers/main.yml - name: restart service service: name: nginx state: restarted #裝備兩個(gè)template文件 [root@ansible-rocky roles]$ cat nginx/templates/nginx.conf.j2 user nginx; worker_processes {{ ansible_processor_vcpus*2 }}; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}'; # logging access_log {{install_dir}}/logs/access-json.log access_json; error_log {{install_dir}}/logs/error.log warn; keepalive_timeout 65; include {{install_dir}}/conf/conf.d/*.conf; } [root@ansible-rocky roles]$ cat nginx/templates/nginx.service.j2 [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile={{install_dir}}/logs/nginx.pid ExecStartPre=/bin/rm -f {{install_dir}}/logs/nginx.pid ExecStartPre={{install_dir}}/sbin/nginx -t ExecStart={{install_dir}}/sbin/nginx ExecReload=/bin/kill -s HUP \$MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true LimitNOFILE=100000 [Install] WantedBy=multi-user.target #在hosts文件中定義wensrvs需要的變量 [root@ansible-rocky opt]$ cat hosts_nginx [websrvs] 10.0.0.18 [websrvs:vars] version="1.22.1" url="http://nginx.org/download/nginx-{{ version }}.tar.gz" install_dir="/apps/nginx" #在playbook中調(diào)用角色 [root@ansible-rocky opt]$ cat nginx_role.yml - hosts: websrvs remote_user: root roles: - nginx #運(yùn)行playbook [root@ansible-rocky opt]$ ansible-playbook -i hosts_nginx nginx_role.yml PLAY [websrvs] **************************************************************************************** TASK [Gathering Facts] ******************************************************************************** ok: [10.0.0.18] TASK [nginx : add group nginx] ************************************************************************ changed: [10.0.0.18] TASK [nginx : add user nginx] ************************************************************************* changed: [10.0.0.18] TASK [nginx : install dependent package] ************************************************************** changed: [10.0.0.18] => (item=gcc) ok: [10.0.0.18] => (item=make) changed: [10.0.0.18] => (item=pcre-devel) changed: [10.0.0.18] => (item=openssl-devel) ok: [10.0.0.18] => (item=zlib-devel) changed: [10.0.0.18] => (item=perl-ExtUtils-Embed) TASK [nginx : get nginx source] *********************************************************************** changed: [10.0.0.18] TASK [nginx : compile and install] ******************************************************************** changed: [10.0.0.18] TASK [nginx : config file] **************************************************************************** changed: [10.0.0.18] TASK [nginx : create directory] *********************************************************************** changed: [10.0.0.18] TASK [nginx : change install directory owner] ********************************************************* changed: [10.0.0.18] TASK [nginx : copy service file] ********************************************************************** changed: [10.0.0.18] TASK [nginx : check config] *************************************************************************** ok: [10.0.0.18] TASK [nginx : start service] ************************************************************************** changed: [10.0.0.18] RUNNING HANDLER [nginx : restart service] ************************************************************* changed: [10.0.0.18] PLAY RECAP ******************************************************************************************** 10.0.0.18 : ok=13 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在被控制端檢查是否安裝完成
2 編譯安裝參數(shù)詳解
編譯安裝參數(shù)示例:
./configure --prefix={{install_dir}} \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module
在編譯安裝參數(shù)中--with開頭的選項(xiàng)
默認(rèn)是禁用的,想要使用的話就需要在編譯的時(shí)候加上;without開頭的選項(xiàng)
默認(rèn)是開啟的,不想要啟用此模塊的話就需要在編譯的時(shí)候加上。
通用配置選項(xiàng)參數(shù):
選項(xiàng) | 解釋說(shuō)明 |
---|---|
--prefix=<path> | Nginx安裝的根路徑,所有其它路徑都要依賴該選項(xiàng) |
--sbin-path=<path> | 指定nginx二進(jìn)制文件的路徑,沒指定的話 這個(gè)路徑依賴<prefix>選項(xiàng) |
--conf-path=<path> | 命令行未指定配置文件,將會(huì)通過(guò)這里指定的路徑加載配置文件 |
--error-log-path=<path> | 寫入錯(cuò)誤日志文件地址,默認(rèn)值:<prefix>/logs/error.log。安裝后,可以使用 nginx.conf 中的 error_log 指令更改。 |
--pid-path=<path> | nginx master進(jìn)程pid寫入的文件位置,默認(rèn)值:<prefix>/logs/nginx.pid。安裝后,可以使用 nginx.conf 中的 pid 指令更改。 |
--lock-path=<path> | 共享存儲(chǔ)器互斥鎖文件路徑 |
--user=<user> | nginx 運(yùn)行用戶。默認(rèn)值:nobody。安裝后,可以使用 nginx.conf 中的 user 指令更改。 |
--group=<group> | nginx 運(yùn)行組。默認(rèn)值:--user 指定的值。安裝后,可以使用 nginx.conf 中的 user 指令更改。 |
默認(rèn)開啟的模塊
選項(xiàng) | 解釋說(shuō)明 |
---|---|
--without-http_gzip_module | 禁用 ngx_http_gzip_module 模塊 |
--without-http_userid_module | 禁用 ngx_http_userid_module 模塊,該模塊設(shè)置適用于客戶端標(biāo)識(shí)的 cookie |
--without-http_access_module | 禁用 ngx_http_access_module 模塊,該模塊允許限制對(duì)某些客戶端地址的訪問(wèn) |
--without-http_rewrite_module | 禁用 URL 轉(zhuǎn)發(fā)(rewrite) |
--without-http_proxy_module | 禁用 HTTP 服務(wù)器代理(proxy)模塊 |
--without-http-cache | 禁用 HTTP 緩存 |
默認(rèn)未開啟模塊
選項(xiàng) | 解釋說(shuō)明 |
---|---|
--with-http_ssl_module | 啟用 HTTPS 協(xié)議支持,需要 OpenSSL 庫(kù)。默認(rèn)情況下未構(gòu)建此模塊 |
--with-http_v2_module | 啟用 HTTP/2 協(xié)議支持。默認(rèn)情況下未構(gòu)建此模塊。 |
--with-http_realip_module | 啟用 ngx_http_realip_module 模塊的功能,該模塊將客戶端地址更改為在指定的 "header " 字段中發(fā)送的地址。默認(rèn)情況下未構(gòu)建此模塊 |
--with-http_sub_module | 啟用 ngx_http_sub_module 模塊,該模塊通過(guò)將一個(gè)指定的字符串替換為另一個(gè)指定的字符串來(lái)修改響應(yīng)。默認(rèn)情況下未構(gòu)建此模塊 |
--with-http_gzip_static_module | 啟用 ngx_http_gzip_static_module 模塊,該模塊支持發(fā)送擴(kuò)展名為 “.gz” 的預(yù)壓縮文件,而不是常規(guī)文件。默認(rèn)情況下未構(gòu)建此模塊 |
--with-http_auth_request_module | 啟用 ngx_http_auth_request_module 模塊,該模塊基于子請(qǐng)求的結(jié)果實(shí)現(xiàn)客戶端授權(quán)。默認(rèn)情況下未構(gòu)建此模塊 |
--with-http_stub_status_module | 啟用 ngx_http_stub_status_module 模塊,該模塊提供對(duì)基本狀態(tài)信息的訪問(wèn)。默認(rèn)情況下未構(gòu)建此模塊 |
--add-module=path | 啟用外部模塊 |
--add-dynamic-module=path | 啟用外部動(dòng)態(tài)模塊 |
--modules-path=path | nginx 動(dòng)態(tài)模塊的目錄。默認(rèn)值:<prefix>/modules目錄 |
perl模塊相關(guān)選項(xiàng)參數(shù)
選項(xiàng) | 解釋說(shuō)明 |
---|---|
--without-pcre | 禁用PCRE庫(kù) |
--with-pcre | 強(qiáng)制使用PCRE庫(kù) |
郵件模塊相關(guān)配置選項(xiàng)參數(shù)
選項(xiàng) | 解釋說(shuō)明 |
---|---|
--with-mail | 激活POP3/IMAP4/SMTP代理模塊,默認(rèn)未激活 |
--with-mail_ssl_module | 允許ngx_mail_ssl_module模塊這個(gè)模塊使得POP3/IMAP/SMTP可以使用SSL/TLS.配置已經(jīng)定義了HTTP SSL模塊,但是不支持客戶端證書檢測(cè) |
--without-mail_pop3_module | 啟用mail模塊后,單獨(dú)禁用pop3模塊 |
--without-mail_imap_module | 啟用mail模塊后,單獨(dú)禁用imap模塊 |
--without-mail_smtp_module | 啟用mail模塊后,單獨(dú)禁用smtp模塊 |
--without-http | 完全禁用http模塊,如果只想支持mall,可以使用此項(xiàng)設(shè)置 |
--with-openssl=DIR | 設(shè)定OpenSSL庫(kù)文件路徑 |
stream模塊相關(guān)參數(shù)
選項(xiàng) | 解釋說(shuō)明 |
---|---|
--with-stream | 開啟stream模塊 |
--with-stream_ssl_module | 啟用 stream 模塊的 SSL/TLS 協(xié)議支持。構(gòu)建和運(yùn)行此模塊需要 OpenSSL 庫(kù)。默認(rèn)情況下未構(gòu)建此模塊 |
--with-stream_realip_module | 啟用 ngx_stream_realip_module 模塊的功能,該模塊將客戶端地址更改為 PROXY 協(xié)議標(biāo)頭中發(fā)送的地址。默認(rèn)情況下未構(gòu)建此模塊 |
--without-stream_access_module | 禁用 ngx_stream_access_module 模塊,該模塊允許限制對(duì)某些客戶端地址的訪問(wèn) |
相關(guān)文章
配置nginx 重定向到系統(tǒng)維護(hù)頁(yè)面
今天抽時(shí)間給大家普及nginx 重定向到系統(tǒng)維護(hù)頁(yè)面的配置內(nèi)容,nginx重定向問(wèn)題說(shuō)起來(lái)也很簡(jiǎn)單,因?yàn)橹囟ㄏ蚝笾苯犹D(zhuǎn)到靜態(tài)頁(yè)面,不需要后續(xù)操作和記錄,所以直接301永久重定向。今天簡(jiǎn)單給大家介紹配置方法,一起看看吧2021-06-06Nginx反向代理和內(nèi)容替換模塊實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容動(dòng)態(tài)替換功能
Nginx是一款輕量級(jí)高性能服務(wù)器軟件,雖然輕量,但功能非常強(qiáng)大,可用于提供WEB服務(wù)、反向代理、負(fù)載均衡、緩存服務(wù)、甚至可以通過(guò)添加一些模塊搭建rtmp流媒體服務(wù),最近碰到一個(gè)客戶需求,需要用到nginx反向代理替換網(wǎng)頁(yè)內(nèi)容,貼出來(lái)跟大家交流,如有不足之處請(qǐng)指出2024-10-10配置Nginx實(shí)現(xiàn)訪問(wèn)本地靜態(tài)資源的完整指南
Nginx 是一個(gè)高性能的 HTTP 服務(wù)器和反向代理服務(wù)器,廣泛用于靜態(tài)資源的托管和負(fù)載均衡,在開發(fā)和生產(chǎn)環(huán)境中,我們常常需要使用 Nginx 來(lái)提供本地靜態(tài)資源的訪問(wèn),本文將詳細(xì)介紹如何配置 Nginx 以便訪問(wèn)本地靜態(tài)資源,需要的朋友可以參考下2024-08-08fastdfs+nginx集群搭建的實(shí)現(xiàn)
這篇文章主要介紹了fastdfs+nginx集群搭建的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Nginx配置多臺(tái)機(jī)器實(shí)現(xiàn)負(fù)載均衡的教程詳解
這篇文章主要為大家詳細(xì)介紹了Nginx配置多臺(tái)機(jī)器實(shí)現(xiàn)負(fù)載均衡的相關(guān)教程,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03nginx日志中添加請(qǐng)求的response日志(推薦)
這篇文章主要介紹了nginx日志中添加請(qǐng)求的response日志,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05