NCNU-OpenSource
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: 1091, lsa --- - Book mode https://hackmd.io/@ncnu-opensource/book Week 06(2020/10/29) === - `/etc/services`: 可以查詢 port 主要是什麼服務 - port 通訊埠 : 一個主機在選擇要接哪個服務的時候,是透過 port 來連接,要資源 - http: 80 - 明文進行傳輸 - https : 443 - 利用 TLS/SSL 加密後的密文傳輸 - 1024 以下的 port 是預留的,分配給固定服務 - 1024 以上為可以動態分配 - 要有 root 權限才能修改 - 規定常用服務是使用什麼 port ,是為了讓 client 端可以容易的找到服務 - web server - 已經有很多常見的服務, apache 、 nginx 、 lighttpd - 回應 http 的 request(?? - 不同的牌子的 web server 可以解決不同的問題 - 常見的web server: - nginx - 可以同時承受很大的流量(Concurrent connection) - 也可以做轉發 - lighttpd - 適合做檔案傳輸 - 支援 cgi, fastcgi - Apache2 - 歷史最悠久 - 市占率最高 - 支援模組,設定較困難/複雜 - 占用硬體資源多 - 設定最麻煩的 |Nginx|Lighttpd|Apache2| |---|---|---| |適合做 Reverse proxy <br> 可以同時承受很大的流量(Concurrent connection)<br>也可以做轉發|適合檔案傳輸<br>支援 cgi, fastcgi|歷史悠久<br>市占率最高<br>模組支援完整<br>軟體肥大占用硬體資源多<br>設定複雜| - 為甚麼要提供不同的 server? - 因為他們被用來在不同的情況 * [virtual host](https://zh.wikipedia.org/wiki/%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA) > 指在同一機器上運行多個網站 * name-based > 在同一機器、IP 位址,架設多個網域名稱的網站 * IP-based > 在同一機器、不同的IP位址,架設多個網站 * port-based > port不同 ex:127.0.0.1:8080、127.0.0.1:8081 ### 實作Nginx - 使用apt安裝Nginx: - `sudo apt install nginx` - 查看Nginx目前的狀況: - `sudo service nginx status` - 預設是開啟的 #### 操作server的指令 (三個選項可以做選擇) - `sudo service [服務名稱] [動作]`: - ex, `sudo service nginx stop` - ![](https://i.imgur.com/ipNsISE.png) - `start` 開啟服務 - `stop` : 關閉服務 - `reload` : 不停止服務,重載設定檔 - `restart` : 停止服務重新開啟 - `status` : 查看服務狀態 - `sudo systemctl [動作] [服務名稱]` - 透過systemctl查看服務狀態 - 先帶操作再帶服務的名字 - ex, `sudo systemctl status nginx.service` 跟`sudo service nginx status` 結果一樣 > 為什麼這兩個結果會是一致的,有沒有什麼辦法可以驗證? > [name=Jerry Wang] - `sudo /etc/init.d/nginx status` - nginx 的操作 - 裡面一能查看指令 - `ls -l /etc/init.d/{script_name}`(操作) - 直接跟nginx說要查看指令 - `sudo systemctl {操作} {unit_name}` - `/lib/systemd/system` - `:w !sudo tee %`: ~~更改伺服器讀取的index順序~~(錯ㄌ) - 解決臨時權限不足的問題 - 安裝多個伺服器時,index會被覆蓋過去,故需去更改讀取順序 - 指令小解釋 - ```:w !```: 執行外部命令 - ```sudo tee```: 提昇tee的權限並且運用tee的功能,也就是把後面的引數印出來或存某個檔案內 - ```%```: 是vim的暫存檔,存檔案路徑 <!-- - `` --> - 於網址輸入`127.0.0.1`: 連上Nginx服務 - 透過 host name 連上 Server 的服務 - ![](https://i.imgur.com/ayZpou4.png) - `vim /etc/hosts `: 記錄這台機器上 host 對應domain name,host name 的相關資訊 - 跑server時,優先查看這個檔案中IP跟hostname的對應 - `http:// {hostname}` or `{hostname}` - 可以存取到同樣的資源 - 已經自動轉成 ip - 根據`/etc/hosts` - 預設檔案內會有`127.0.0.1 localhost` - 預設檔案內會有`127.0.1.1 <電腦名稱>` - 因此打上 `http://localhost` 和 `http://<電腦名稱>` 會自動跳轉 - 網址上輸入 `localhost` 也可以出現一樣的頁面 - 如果要用`{hostname}`進去,要在`sudo vim /etc/hosts `新增`127.0.0.1 {hostname}`才可使用 - `127.0.0.1 netmask 255.0.0.0`: 127 之下的都是自己的 #### Nginx 相關設定 ==nginx 設定檔主要會放在 3 個地方== - `vim /etc/nginx/nginx.conf`: 主設定檔 - 包含了服務的相關設定(log 檔等等) - `etc`: Nginx(要載入的其他)設定檔 - 在此目錄下操作時,記得加上`sudo` - 盡量不要寫設定在這裡,主要裝系統設定,讓它隨著系統更新而更新 - 路徑&查看 : `username@hostname:/etc/nginx$`+`cat nginx.conf` - nginx讀nginx.conf,若有include再一一load進來![](https://i.imgur.com/upAm2Fo.png) - `available` & `enabled` 區隔開來以免在系統設定時互相影響 - `sites-available`:設定檔的存放庫,設定檔都先放這,要運行時直接softlink到`sites-enabled` - 當成設定檔的存放庫,以便管理 - `sites-enabled`: 實際讓服務運作的設定檔 - 如上所述sites-enable/default 連結到 sites-available/default - server在運作時,需要reload,才會更新設定 - ~~當成設定檔的存放庫,以便管理~~(郭助教說這是錯的) - `sudo netstat -ntupl`: 查看服務跑在哪個port上面(目前的網路狀況) - 目前的網路狀況,哪些port是被listen的 - ![](https://i.imgur.com/2rmIdfb.png)可看到Nginx是run在80 port - `netstat -ntul`: 不會顯示特定服務被哪個port listen #### Virtual host 設定 - `ls -l /etc/nginx/sites-available`:查看`sites-available`檔案 - `vim /etc/nginx/sites-available/default` - `sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup`: 先複製一份,避免被改壞了,還可以復原 :::info 要在 `etc` 下操作,都要 `sudo` ::: > 好像在 / (根目錄) 底下做操作都要 `sudo` [name=陳柏瑋] > ![](https://i.imgur.com/E4N6QQD.png =400x)<br> 在etc下的檔案權限幾乎都是root,所以建議都用sudo執行 [name=呂心慈] <!-- - `vim /etc/nginx/default`: --> - `sudo nginx -t`: 在`enable`的設定檔可知道有無問題![](https://i.imgur.com/xlZ3bts.png) - **練習**: - 將 Nginx 預設聽的 port 從 80port 改到 8080port - 在這更改 `sudo vim /etc/nginx/sites-available/default` - `sudo service nginx restart`: 將更改過的設定更新至server - 當對服務的port做更改時,建議先停止server `sudo service nginx stop`,再開始 `sudo service nginx start` - 更改完後,http://127.0.0.1:80 應無法連接,需至 http://127.0.0.1:8080 - 如果:80連得上,可能是瀏覽器自動幫你轉的,可以用無痕試看看 - 用無痕模式就連不上了 #### Virtual Host 介紹 - 利用 server 開頭來區分每一台 Virtual Host 的內容 - root : 設定網頁的根目錄 - index : 設定搜尋順序 - location : 對usr輸入的路徑做何種操作 - `try_files`: - 根據目錄,給予不一樣的操作 - 按順序檢查文件是否存在 - 查看`root`目錄下是否有相應的檔案名稱可提供給使用者 - 若`root`中沒有的話,則去`index`中查看 - 若`root` & `index` 皆沒有找到相應的檔案名稱,會回傳404 ### 透過不同Domain name 導向不同 virtual host :::info 連結到 http://nginx1.com:8080 的流量會被導向 網站根目錄在 /var/www/vhost/nginx1/ 的 virtual host ::: - `sudo vim /etc/hosts`: 更改host的紀錄 - 例子: `127.0.0.1 nginx1.com` - `sudo mkdir /var/www/vhost`: 於 `/var/www` 下,建立vhost資料夾 - `sudo mkdir /var/www/vhost/nginx1`: 在 vhost 下,再建立一個 nginx1 資料夾,用來存放`nginx1`的所有資源 - `ls -l /var/www/vhost/` : check 一下 ==回到根目錄== - 修改 config 設定 - `sudo vim /etc/nginx/sites-available/nginx1` ```shell= server { listen 8080; # port server_name nginx1.com; # domain name root /var/www/vhost/nginx1; #網頁根目錄位置 從這裡開始下去找 index test.html; #使用者的url沒帶參數時,直接找此檔案 location / { try_files $uri $uri/ =404; #沒有使用者要的檔案時,回傳404(`$uri`: 檔案名,`$uri/`:檔案目錄) } } ``` ==以上完成還沒被啟用== #### 啟用 Nginx1 - `sudo ln -s /etc/nginx/sites-available/nginx1 /etc/nginx/sites-enabled/`: 把nginx1連結(soft link)到enabled,讓enabled啟用nginx1 - `ls -l /etc/nginx/sites-enabled/` : check 一下![](https://i.imgur.com/ZGM8BS0.png) - `sudo service nginx reload`:麻煩 nginx 在 enable 把裡面的設定檔去吃進來 - `sudo vim test.html`: 寫一個HTML檔進行測試(寫在/var/www/vhost/nginx1/裡面) ```html= <h1>nginx1</h1> ``` - 在網址列輸入 `http://nginx1.com:8080` ### Proxy Pass Request #### 新增host紀錄 - 希望透過不同的 domain name 連結到不同網站 - `sudo vim /etc/hosts` - `127.0.0.1 nginx2.proxy.com` #### **修改 Config 設定** - `sudo vim /etc/nginx/sites-available/nginx2` ```s= server { listen 8080; # 聽 ss8080 port server_name nginx2.proxy.com; location / { proxy_pass https://www.google.com; # 導到google } } ``` ==如果是nginx要proxy到lsa.bluet.org結果出現502 bad gateway的error訊息的話可以給它指定外面的DNS server喔== ```s= server { listen 8085; server_name nginx2.proxy.com; location / { resolver 8.8.8.8; set $example lsa.bluet.org; proxy_pass http://$example; } } ``` > apache proxy conf: > ```s= > <VirtualHost *:8090> > ServerName myapache.com > ProxyPreserveHost On > ProxyRequests Off > <Proxy *> > Order deny,allow > Allow from all > </Proxy> > > <Location /> > ProxyPass http://xxxxxx.com:8091/ > ProxyPassReverse http://xxxxxx.com:8091/ > </Location> ></VirtualHost> > > ==記得把proxy module打開== > - `sudo a2enmod proxy` > - `sudo a2enmod proxy_http` - `sudo ln -s /etc/nginx/sites-available/nginx2 /etc/nginx/sites-enabled`: softlink 到 `sites-enabled` - `sudo nginx -t` : 檢查有沒有錯 - `sudo service nginx reload` : 把裡面的設定檔去吃進來 - 在網址輸入 `http://nginx2.proxy.com:8080` #### 補充 tee - `ps | tee lala` : 把 ps 這個指令的輸出存到 lala 裡並顯示出來 - `tee` : 將結果同時輸出到螢幕和檔案 - `ps | sudo tee /etc/lala` - `sudo rm /etc/lala` : 記得刪掉喔,避免以後出錯 ### lighttpd - `sudo apt install lighttpd`: 先安裝 `lighttpd` - `sudo vim /etc/lighttpd/lighttpd.conf` : 查看 `lighttpd.conf` 這個設定檔 - `modules`: 模組 - ~~`lighttpd`: 查看現在有哪些module可以用~~ (這個不能查看喔 by助教) - `force-reload`: 使用這個指令,讀入改變的設定 - `lighttpd-enable-mod`: 查看現在有哪些module可以用 - modules 的其中一種模組 - ![](https://i.imgur.com/R8Ox3mt.png)輸入ssl - `sudo lighttpd-disable-mod` 就 disable - `cat /etc/passwd | grep www-data` : 在 /etc/passwd 裡面找 `www-data` 這個關鍵字在哪裡 #### lighttpd.conf - `url.access-deny` 不能被訪問 - `static-file.exclude-extensions` 不能被下載

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully