1、Linux安裝nginx
1.1、yum安裝
可以使用yum直接安裝nginx:yum -y install nginx,如果安裝報(bào)錯(cuò),可以換個(gè)yum源試試
源碼安裝提供更大的靈活性,但需要更多的手動(dòng)管理,而Yum安裝則更方便
yum源鏡像站,阿里云的推薦:https://developer.aliyun.com/mirror/
常用的鏡像:epel和centos,根據(jù)提示命令直接安裝即可
然后執(zhí)行:yum clean all、和yum makecache
1.2、源碼安裝
我的系統(tǒng)版本:centos7.9
安裝的nginx版本:nginx-1.25.3
1、下載安裝包后上傳到主機(jī),或直接wget下載:wget http://nginx.org/en/download.html/nginx-1.25.3
2、進(jìn)行解壓:tar zxf nginx-1.25.3.tar.gz,然后進(jìn)入nginx目錄:cd nginx-1.25.3
3、安裝依賴(lài):yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
4、配置configure --prefix 代表安裝的路徑,–with-http_ssl_module 安裝ssl,–with-http_stub_status_module:
./configure --prefix=/usr/local/nginx-1.25.3 --with-http_ssl_module --with-http_stub_status_module
5、編譯安裝:make & make install
6、進(jìn)入安裝的目錄,并啟動(dòng)nginx:cd /usr/local/nginx-1.25.3/sbin,啟動(dòng):./nginx
7、配置變量:cp /usr/local/nginx-1.25.3/sbin/nginx /usr/bin/
系統(tǒng)變量如下:
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
8、這時(shí)候就可以直接訪問(wèn)試試是否正常了
1.3、配置nginx開(kāi)機(jī)自啟
配置開(kāi)機(jī)自啟程序:建議在/etc/systemd/system/目錄下配置對(duì)應(yīng)文件
1、vi /etc/systemd/system/nginx.service
2、編輯參數(shù)
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx-1.25.3/sbin/nginx -t -c /usr/local/nginx-1.25.3/conf/nginx.conf
ExecStart=/usr/local/nginx-1.25.3/sbin/nginx -c /usr/local/nginx-1.25.3/conf/nginx.conf
ExecReload=/usr/local/nginx-1.25.3/sbin/nginx -s reload
ExecStop=/usr/local/nginx-1.25.3/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
含義
[Unit] 部分:
Description(描述): 服務(wù)的可讀性描述。
After(在之后啟動(dòng)): 定義在啟動(dòng)此單元之后應(yīng)該啟動(dòng)的其他單元。
[Service] 部分:
Type(類(lèi)型): 指定服務(wù)的類(lèi)型。forking 類(lèi)型是指當(dāng)服務(wù)啟動(dòng)時(shí),主進(jìn)程會(huì)派生(fork)出一個(gè)或多個(gè)子進(jìn)程。
ExecStartPre(啟動(dòng)前的命令): 在實(shí)際啟動(dòng) Nginx 之前執(zhí)行的命令,用于檢查配置文件是否正確。在這里,
-t 參數(shù)表示測(cè)試配置文件的語(yǔ)法。
ExecStart(啟動(dòng)命令): 實(shí)際啟動(dòng) Nginx 的命令。-c 參數(shù)用于指定配置文件的路徑。
ExecReload(重載命令): 用于重新加載 Nginx 配置的命令。這里使用 -s reload 參數(shù)。
ExecStop(停止命令): 停止 Nginx 的命令。這里使用 -s stop 參數(shù)。
PrivateTmp(私有臨時(shí)目錄): 將服務(wù)的臨時(shí)目錄設(shè)為私有的,以增加安全性。
[Install] 部分:
WantedBy(被期望在哪個(gè)目標(biāo)中啟用): 指定服務(wù)應(yīng)該在哪個(gè)系統(tǒng)目標(biāo)中啟動(dòng)。multi-user.target 表示在多用戶(hù)模式下啟用。
如果你想替換 Nginx 的路徑,將所有 /usr/local/nginx-1.25.3 替換為你實(shí)際的安裝路徑即可。
確保路徑和文件名與你的實(shí)際安裝一致
3、systemctl daemon-reload
4、sudo systemctl enable nginx
5、sudo systemctl start nginx
6、sudo systemctl status nginx,查看狀態(tài)是否正常
注:這時(shí)候你執(zhí)行第5步的時(shí)候會(huì)報(bào)錯(cuò),因?yàn)橹耙呀?jīng)啟動(dòng)nginx了,會(huì)提示端口占用,解決方法,kill掉占用的pid,或直接重啟即可
1.4、配置域名和重定向
1、編輯配置文件:vi /usr/local/nginx-1.25.3/conf/nginx.conf
找到代碼塊:
server {
listen 80;
server_name 這里寫(xiě)域名;
return 301 https://blog.csdn.net/weixin_54106903?type=blog;
}
return 301 重定向url;
2、檢查nginx配置文件是否正確:nginx -t
3、然后重載nginx服務(wù):nginx -s reload
4、訪問(wèn)測(cè)試即可:curl http://域名,或加-L訪問(wèn)重定向后的界面
1.5、配置多域名訪問(wèn)
1、編輯配置文件:vi /usr/local/nginx-1.25.3/conf/nginx.conf
找到代碼塊,我是重新加了一個(gè)server代碼塊,如果每個(gè)域名需要不同的處理邏輯或安全策略,使用不同的 server 塊可能更清晰:
代碼塊1:
server {
listen 80;
server_name A123;
return 301 https://blog.csdn.net/weixin_54106903?type=blog;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
代碼塊2:
server {
listen 80;
server_name B123;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、nginx -s reload
3、測(cè)試:curl http://域名
1.6、如何配置反向代理
1、編輯配置文件:vi /usr/local/nginx-1.25.3/conf/nginx.conf
server {
listen 80;
server_name 域名;;
location / {
# root html;
# index index.html index.htm;
proxy_pass https://www.baidu.com:443;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
proxy_pass url; 即可
2、systemctl restart nginx
3、驗(yàn)證
1.7、nginx配置IPv6
1、先確認(rèn)你的域名解析到了服務(wù)器的IPv6地址
2、編輯配置文件:vi /usr/local/nginx-1.25.3/conf/nginx.conf
server {
listen [::]:80;
server_name v6.cjzzrr.cn;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3、重啟nginx:systemctl restart nginx
4、查看是否有tcp6格式的80監(jiān)聽(tīng)
5、測(cè)試訪問(wèn):curl -6 http://v6.cjzzrr.cn
或使用,實(shí)現(xiàn)確認(rèn)你主機(jī)有v6環(huán)境才能訪問(wèn)v6網(wǎng)站:curl -6g http://[IPv6地址]
2、nginx如何配置https
2.1、申請(qǐng)證書(shū)
推薦使用阿里云的證書(shū):
2.2、配置nginx
1、先在conf目錄下創(chuàng)建cert目錄:/usr/local/nginx-1.25.3/conf/cert
2、上傳證書(shū),因?yàn)樽C書(shū)文件較小,我用的rz上傳的,yum -y install lrzsz,然后使用rz選擇你本地的證書(shū)上傳:
3、編輯配置文件:vi /usr/local/nginx-1.25.3/conf/nginx.conf
參數(shù)如下:
server {
listen 443 ssl;
server_name www.cjzzrr.cn;
ssl_certificate /usr/local/nginx-1.20.2/conf/cert/www.cjzzrr.cn.pem;
ssl_certificate_key /usr/local/nginx-1.20.2/conf/cert/www.cjzzrr.cn.key;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4、重啟服務(wù):systemctl restart nginx
5、驗(yàn)證:正確
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-830576.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-830576.html
到了這里,關(guān)于yum安裝nginx、源碼安裝、nginx開(kāi)機(jī)自啟、配置多域名與重定向、反向代理、配置IPv6、獲取證書(shū)與配置證書(shū)https的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!