安裝
Ubuntu
apt-get install nginx
CentOS
# 1. 安裝相關依賴 yum install gcc -c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel # 2. 下載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 # 3. 編譯 ./configure # 這個命令會在目錄里生成Makefile文件 make make install # 安裝路徑為/usr/local/nginx # 執行文件路徑/usr/local/nginx/sbin/nginx,可以做軟連接 # 啟動、停止、關閉命令 nginx -s reload nginx -s stop nginx -s quit
相關命令
相關目錄
# 主目錄 /usr/local/nginx # docker部署的nginx的主目錄默認為/etc/nginx # 配置文件路徑 /usr/local/nginx/conf/nginx.conf # linux 安裝目錄\conf\nginx.conf # Windows # 靜態文件路徑 /usr/local/nginx/html/
啟動
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf # 看到如下顯示說明啟動成功 ''' nginx.conf syntax is ok nginx.conf test is successful '''
重啟
/usr/local/nginx/sbin/nginx -s reload
查看進程是否啟動
ps -ef|grep nginx
查看端口號是否占用
netstat -tunlp|grep 端口號
強制停止
pkill -9 nginx
驗證nginx配置文件是否正確
/usr/local/nginx/sbin/nginx -t # 看到如下顯示說明配置文件正確 ''' nginx.conf syntax is ok nginx.conf test is successful '''
Nginx配置基於多域名、端口、IP的虛擬主機
基於域名的虛擬主機
通過不同的域名區分不同的虛擬主機,基於域名的虛擬主機是企業應用最廣的虛擬主機類型,幾乎所有對外提供服務的網站使用的都是基於域名的主機
配置:修改nginx配置文件配置多域名,重啟nginx服務,創建對應的不同站點目錄並上傳站點文件,也可都使用一個站點目錄,通過多域名來訪問

server{ listen 80; server_name www.aaa.com; location / { root html/aaa; index index.html index.htm; } } server{ listen 80; server_name www.bbb.com; location / { root html/bbb; index index.html index.htm; } } server{ listen 80; server_name www.ccc.com; location / { root html/ccc; index index.html index.htm; } }
基於端口的虛擬主機
通過不同的端口來區分不同的虛擬主機,此類虛擬主機對應的企業應用主要為公司內部的網站,例如:一些不希望直接對外提供用戶訪問的網站后台等,訪問基於端口的虛擬主機,地址里要帶有端口號,例如http://www.test.com:81 http://www.test.com:82等

server{ listen 80; server_name localhost; location / { root html/aaa; index index.html index.htm; } } server{ listen 81; server_name localhost; location / { root html/bbb; index index.html index.htm; } } server{ listen 82; server_name localhost; location / { root html/ccc; index index.html index.htm; } }
基於IP的虛擬主機
通過不同的IP區分不同的虛擬主機,此類虛擬主機對應的企業應用非常少見,一般不同的業務需要使用多IP的場景都會在負載均衡上進行IP綁定。

server{ listen 10.0.0.7:80; server_name www.aaa.com; location / { root html/aaa; index index.html index.htm; } } server{ listen 10.0.0.8:80; server_name www.bbb.com; location / { root html/bbb; index index.html index.htm; } } server{ listen 10.0.0.9:80; server_name www.ccc.com; location / { root html/ccc; index index.html index.htm; } }
其他配置
修改上傳文件大小限制
Nginx 默認是對上傳的文件大小有限制的,我們可以修改相應配置來修改對文件大小的限制
# 在http{}段落增加如下參數 client_max_body_size 20M; # 20M可以換為任何大小
參考:https://www.cnblogs.com/ssgeek/p/9220922.html