# **Nginx 入門** :::info Nginx是C語言編寫的伺服器程式,可作為WEB伺服器、反向代理、負載均衡元件、郵件代理以及HTTP緩存伺服器,初始作者為Igor Sysoev,首次發佈版本為2004年10月4日,Nginx是免費的開源軟體,根據類BSD授權條款釋出,可以運行在多種BSD,HP-UX,IBM AIX,Linux MacOS,Solaris,Windows。 Nginx初衷為解決WEB伺服器C-10K問題,即併發連接1萬,在執行緒模型下難以實現,Nginx使用Linux 內核的非同步事件驅動機制 (EPOLL)構建出高性能,輕量化的伺服器 ![](https://i.imgur.com/7oeaGh2.png) ::: ## **1. Nginx 安裝** (1) 使用軟件倉安裝 ```bash= #On Rocky Linux yum list nginx yum install nginx systemctl start nginx #On Ubuntu Linux apt search nginx apt install nginx service nginx restart ``` 註 : 設定檔路徑 : /etc/nginx <br> (2) 使用源碼安裝 > 安裝 Nginx 所需工具 ```bash= #On Rocky Linux yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel unzip gcc ``` > 下載套件 ```bash= mkdir ~/working; cd ~/working wget http://nginx.org/download/nginx-1.20.2.tar.gz mkdir build; cd ./build git clone https://github.com/arut/nginx-rtmp-module.git tar zxvf ../nginx-1.20.2.tar.gz ``` > 編輯 Nginx ```bash= ./configure --prefix=/opt/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module make make install ``` > 服務設定 ```bash= vi /etc/systemd/system/nginx.service ``` ``` [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/opt/nginx/sbin/nginx -t ExecStartPost=/bin/sleep 0.1 ExecStart=/opt/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=mixed PrivateTmp=true [Install] WantedBy=multi-user.target ``` > 服務啟動及管理 ```bash= systemctl daemon-reload systemctl enable nginx systemctl start nginx systemctl stop nginx ``` ## **2. Nginx 配置管理** (1) 配置文件 > 配置文件預設路徑 ![](https://i.imgur.com/gm5RM1b.png) > nginx.conf ![](https://i.imgur.com/NcshLnm.png) :::info ![](https://i.imgur.com/mjFxG8L.png) ::: > Virtual server 配置 ``` # inveontory.conf server { listen 80; server_name 10.10.0.201; root /opt/Inventory; index Login.html; } ``` ###### tags: `Web` `Proxy` `Loadbalance`