由于在學習配置時,網(wǎng)上的教程比較雜亂,用時很久才做好一些基礎(chǔ)配置,把流程記錄一下方便和我一樣的小白學習
本文寫于2023.2.10,如果間隔太久,下述內(nèi)容可能會失效,請另尋教程
僅包含基礎(chǔ)教程,個人服務(wù)未涉及到負載均衡
- 安裝nginx
- 配置靜態(tài)服務(wù)器
- 配置端口轉(zhuǎn)發(fā)
- 配置域名
- 配置https
服務(wù)器: 阿里云ubuntu20.04
nginx版本: nginx/1.18.0 (Ubuntu)
1. 安裝nginx
1.1 安裝及常用命令
# 更新apt-get源
sudo apt-get update
# 安裝
sudo apt-get install nginx
# 安裝后將自動開啟nginx服務(wù),打開瀏覽器輸入ip即可查看初始頁面
# 查看安裝版本
nginx -v
# 輸出:nginx version: nginx/1.18.0 (Ubuntu)
# systemctl命令
# 查看狀態(tài)
sudo systemctl status nginx
# 啟動
sudo systemctl start nginx
# 停止
sudo systemctl stop nginx
# 重啟
sudo systemctl restart nginx
注意:對nginx配置文件修改之后,都要重啟nginx服務(wù),加載修改后的配置文件
1.2 文件結(jié)構(gòu)
# 查看文件結(jié)構(gòu)
tree /etc/nginx
/etc/nginx
├── conf.d
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules-available
├── modules-enabled
│ ├── 50-mod-http-image-filter.conf -> /usr/share/nginx/modules-available/mod-http-image-filter.conf
│ ├── 50-mod-http-xslt-filter.conf -> /usr/share/nginx/modules-available/mod-http-xslt-filter.conf
│ ├── 50-mod-mail.conf -> /usr/share/nginx/modules-available/mod-mail.conf
│ └── 50-mod-stream.conf -> /usr/share/nginx/modules-available/mod-stream.conf
├── nginx.conf
├── proxy_params
├── scgi_params
├── sites-available
│ └── default
├── sites-enabled
│ └── default -> /etc/nginx/sites-available/default
├── snippets
│ ├── fastcgi-php.conf
│ └── snakeoil.conf
├── uwsgi_params
└── win-utf
1.3 配置文件內(nèi)容
nginx.conf (為了方便看,我刪掉了初始內(nèi)容中所有帶注釋的代碼)
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
最關(guān)鍵的是下面兩行引入,上面的代碼含義目前我還沒研究,用到再說
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
這兩行的意思是:從conf.d中加載所有后綴為conf的文件,從sites-enabled中加載所有文件,均作為配置文件
sites-enabled文件我用不習慣,因此我注釋掉了這行,使用conf.d做配置文件
在conf.d中添加static.conf
server {
listen 80;
server_name localhost;
charset utf-8; # 防止中文顯示出現(xiàn)亂碼
#access_log logs/host.access.log main;
location / {
root /var/www/html; # 你的靜態(tài)資源路徑
index index.html index.htm;# 訪問的文件為html, htm
}
}
要注意的是,在/var/www/html
目錄中,文件的名字不是index.html
,原名為index.nginx.debian.html
,改成前者即可。
通過三處修改,完成從sites-enable
到conf.d
的遷移
- 在
nginx.conf
中注釋掉include /etc/nginx/sites-enabled/*;
- 在
conf.d
目錄下新建static.conf
,添加如上文件內(nèi)容 - 修改
/var/www/html
目錄中的文件名為index.html
# 檢查配置文件是否有誤
nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
# 重啟服務(wù)
sudo systemctl restart nginx
2. 配置靜態(tài)服務(wù)器
自行尋找一個網(wǎng)頁做測試,上傳到/var/www/html
我上傳了之前寫過的一個markdown在線編輯器,是一個文件夾,文件夾名為markdown
開始修改static.conf
文件
server {
listen 80;
server_name localhost;
charset utf-8; # 防止中文顯示出現(xiàn)亂碼
# 根據(jù)自己需要配置日志文件,可以單獨配置,也可以全部放在/var/log/nginx的日志中
#access_log logs/host.access.log main;
location / {
root /var/www/html; # 你的靜態(tài)資源路徑
index index.html index.htm;# 訪問的文件為html, htm
}
location /markdown {
alias /var/www/html/markdown; # 你的靜態(tài)資源路徑
index index.html index.htm;# 訪問的文件為html, htm
}
# 后續(xù)如果有其他配置,模仿markdown的配置添加即可
# location /example {
# alias /var/www/html/example; # 你的靜態(tài)資源路徑
# index index.html index.htm;# 訪問的文件為html, htm
# }
}
對于多個路徑的配置: [1]
-
使用root會將location后的markdown追加在路徑的尾部,在訪問時就會訪問到/var/www/html/markdown/markdown
-
使用alias則不會將location后的markdown追加在路徑尾部,訪問時就為正確路徑/var/www/html/markdown
如果添加charset utf-8;
后還存在亂碼,強制刷新一下試試
輸入IP/markdown
查看配置結(jié)果
3. 配置端口轉(zhuǎn)發(fā)
自行尋找一個服務(wù)做測試,開在非80端口即可,我開在了8822端口
注:要在服務(wù)器的安全組配置,打開對應(yīng)端口
首先測試一下 IP:8822 能不能正常使用,可以使用說明服務(wù)成功啟動在8822端口,進行后續(xù)配置。
server {
listen 80;
server_name localhost;
charset utf-8; # 防止中文顯示出現(xiàn)亂碼
# 添加頭部信息
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Forwarded-Host $Host;
proxy_set_header proxy_set_Server $Host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 訪問IP/eat,則會自動訪問對應(yīng)地址IP:8822
location /eat/ {
proxy_pass http://localhost:8822/;
}
# 后續(xù)如果有其他配置,模仿eat的配置添加即可
# 訪問IP/example,則會自動訪問對應(yīng)地址IP:port
# location /example/ {
# proxy_pass http://localhost:port/;
# }
}
輸入IP/eat
查看配置結(jié)果
4. 配置域名
我從阿里云購買了域名southyang.cn,將子域名demo.southyang.cn解析到服務(wù)器上
以端口轉(zhuǎn)發(fā)為例:
server {
listen 80;
server_name demo.southyang.cn;
charset utf-8; # 防止中文顯示出現(xiàn)亂碼
# 添加頭部信息
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Forwarded-Host $Host;
proxy_set_header proxy_set_Server $Host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 訪問demo.southyang.cn/eat,則會自動訪問對應(yīng)地址IP:8822
location /eat/ {
proxy_pass http://localhost:8822/;
}
# 后續(xù)如果有其他配置,模仿eat的配置添加即可
# 訪問IP/example,則會自動訪問對應(yīng)地址IP:port
# location /example/ {
# proxy_pass http://localhost:port/;
# }
}
輸入demo.southyang.cn/eat
查看配置結(jié)果
5. 配置https
以端口轉(zhuǎn)發(fā)為例: [2]
server {
listen 80;
server_name demo.southyang.cn;
# 跳轉(zhuǎn)https
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name demo.southyang.cn;
charset utf-8; # 防止中文顯示出現(xiàn)亂碼
# 添加頭部信息
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Forwarded-Host $Host;
proxy_set_header proxy_set_Server $Host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 配置證書
ssl_certificate 證書密鑰地址
ssl_certificate_key 證書公鑰地址
ssl_verify_client off;
proxy_ssl_verify off;
# 訪問demo.southyang.cn/eat,則會自動訪問對應(yīng)地址IP:8822
location /eat/ {
proxy_pass http://localhost:8822/;
proxy_redirect off;
}
# 后續(xù)如果有其他配置,模仿eat的配置添加即可
# 訪問IP/example,則會自動訪問對應(yīng)地址IP:port
# location /example/ {
# proxy_pass http://localhost:port/;
# }
}
輸入demo.southyang.cn/eat
查看配置結(jié)果
注: 目前我只用到了上述所列的內(nèi)容,其他內(nèi)容用到了再學
引用內(nèi)容:
[1] https://blog.csdn.net/qq_39827677/article/details/113745095文章來源:http://www.zghlxwxcb.cn/news/detail-779880.html
[2] https://github.com/Mereithhh/van-nav文章來源地址http://www.zghlxwxcb.cn/news/detail-779880.html
到了這里,關(guān)于Ubuntu20.04安裝配置Nginx的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!