国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Ubuntu20.04安裝配置Nginx

這篇具有很好參考價值的文章主要介紹了Ubuntu20.04安裝配置Nginx。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

由于在學習配置時,網(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-enableconf.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

[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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Ubuntu20.04中Docker安裝與配置

    Ubuntu20.04中Docker安裝與配置

    1、卸載可能存在的舊版本 2、更新apt包索引 顯示“正在讀取軟件包列表… 完成” 3、安裝以下包以使apt可以通過HTTPS使用存儲庫(repository) 顯示“done.” 4、添加Docker官方的GPG密鑰 顯示“OK” 5、設(shè)置stable存儲庫 如果沒有報錯就正常進行下一步。顯示“正在讀取軟件包列表… 完

    2024年02月05日
    瀏覽(52)
  • Linux系列---【Ubuntu 20.04安裝KVM】

    Linux系列---【Ubuntu 20.04安裝KVM】

    1.安裝kvm 2. 將當前用戶添加至libvirt 、 kvm組 3.驗證安裝 4.啟動libvert 至此,kvm安裝完畢! 1.安裝xrdp 2.查看ubuntu的ip 3.打開windows遠程連接工具 登錄成功后,記得修改登錄密碼。不然,系統(tǒng)一鎖屏,就打不開了。通過這里輸入原來ubuntu的root密碼,解不了休眠鎖。 1.安裝virt-manager

    2024年02月16日
    瀏覽(28)
  • linux下ubuntu20.04的 libpcap安裝

    linux下ubuntu20.04的 libpcap安裝

    Libpcap安裝 方式一:下載官方安裝包 需要先安裝: 1、安裝GCC: sudo apt-get install gcc-c++(由于我當前的環(huán)境docker下已安裝了gcc,所以這一步跳過) 2、安裝flex: sudo apt-get install flex 沒有flex,直接安裝libpcap會提示\\\"Your operating system’s lex is insufficient to compile libpcap\\\"錯誤; 3、安裝

    2024年01月17日
    瀏覽(29)
  • Linux學習之Ubuntu 20.04安裝內(nèi)核模塊

    Linux學習之Ubuntu 20.04安裝內(nèi)核模塊

    參考博客:Ubuntu20.04編譯內(nèi)核教程 sudo lsb_release -a 可以看到我當前的系統(tǒng)是 Ubuntu 20.04.4 , sudo uname -r 可以看到我的系統(tǒng)內(nèi)核版本是 5.4.0-100-generic 。 sudo apt-get install -y libncurses5-dev flex bison libssl-dev 安裝所需要的依賴。 sudo apt-get install linux-source 按兩下 Tab ,看一下可以下載的源

    2024年02月15日
    瀏覽(128)
  • Linux Ubuntu20.04深度學習環(huán)境快速配置命令記錄

    Linux Ubuntu20.04深度學習環(huán)境快速配置命令記錄

    1、更新系統(tǒng)包 2、安裝顯卡驅(qū)動 使用apt方式安裝驅(qū)動,多數(shù)情況不容易成功, 使用一下方法更佳: 1.查看合適顯卡的驅(qū)動版本 NVIDIA GeForce 驅(qū)動程序 - N 卡驅(qū)動 | NVIDIA NVIDIA GeForce 驅(qū)動程序官方提供下載最新版的 Geforce 驅(qū)動程序,可提升 PC 游戲體驗和應(yīng)用程序速度。更多關(guān)于更

    2024年02月09日
    瀏覽(26)
  • 20.04Ubuntu中SSH的安裝及配置指南

    ?引言: SSH(Secure Shell)是一種加密協(xié)議,允許遠程安全訪問和管理計算機。在Ubuntu操作系統(tǒng)中,安裝和配置SSH服務(wù)器是一項關(guān)鍵任務(wù),為遠程訪問提供了安全可靠的連接。本文將向您介紹如何在Ubuntu中安裝和配置SSH服務(wù)器。 在Ubuntu中配置SSH(Secure Shell)的作用是實現(xiàn)安全的

    2024年02月08日
    瀏覽(17)
  • Ubuntu20.04(linux)安裝JDK1.8

    Ubuntu20.04(linux)安裝JDK1.8

    Ubuntu20.04安裝JDK1.8 1.下載 : jdk-8u371-linux-x64.tar.gz 2.解壓到指定目錄 3.在/usr/lib/ 創(chuàng)建 jvm 文件夾,并將解壓的jdk移動到/usr/lib/jvm(非root用戶使用sudo) 4.打開/etc/profile,配置環(huán)境變量 vi /etc/profile(編輯文件) Shift + g 將光標定位到行尾 這個時候按一下 Insert 鍵,進入編輯模式 按Esc鍵 輸

    2024年02月12日
    瀏覽(35)
  • Ubuntu 20.04下安裝配置Qt開發(fā)環(huán)境的步驟

    下面是在Ubuntu 20.04下安裝配置Qt開發(fā)環(huán)境的步驟: 安裝Qt Creator 在終端中輸入以下命令以安裝Qt Creator: 在終端中輸入以下命令以安裝Qt 5開發(fā)庫: 安裝g++和gcc編譯工具 配置Qt Creator 打開Qt Creator,進入“Tools”菜單,選擇“Options”,在彈出的對話框中選擇“Build Run”,然后選擇

    2024年02月15日
    瀏覽(38)
  • ubuntu 20.04 4090 顯卡驅(qū)動安裝 深度學習環(huán)境配置

    ubuntu 20.04 4090 顯卡驅(qū)動安裝 深度學習環(huán)境配置

    準備工作: 換源 安裝輸入法:重啟的步驟先不管(自選) sudo apt update sudo apt upgrade 禁用nouveau驅(qū)動(這個驅(qū)動是ubuntu開源小組逆向破解NVIDIA的開源驅(qū)動,與英偉達的原有驅(qū)動不兼容)執(zhí)行完第2.3步,先不重啟。 打開 軟件和更新 , 選擇 附加驅(qū)動,安裝推薦驅(qū)動(第一個),點

    2024年02月16日
    瀏覽(33)
  • Linux(7)Ubuntu20.04 arm64安裝Docker

    Linux(7)Ubuntu20.04 arm64安裝Docker

    vi /etc/apt/sources.list 這個命令后面跟了幾個軟件包的名字,它們分別是: apt-transport-https:這個軟件包允許apt使用HTTPS協(xié)議來訪問軟件源。 ca-certificates:這個軟件包提供了一些受信任的證書頒發(fā)機構(gòu)的證書,用來驗證HTTPS連接的安全性。 curl:這個軟件包提供了一個命令行工具,

    2024年02月10日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包