天天看點

使用nginx配置靜态頁面展示sphinx-doc

文章目錄

  • ​​1. 安裝nginx​​
  • ​​2. 設定靜态頁面請求​​
  • ​​3. 遇到問題的分析​​
  • ​​1. 網頁 403 Forbidden​​
  • ​​2. 網頁 404 Not Found​​
  • 系統: Ubuntu
  • 目的: 使用sphinx-doc給實驗室制作了一套找工作的經驗說明, 但是生成的index嘗試了使用Django挂載特别麻煩(需要修改靜态檔案js, css等的相對連結), 然後發現還可以有兩種方式挂載到網絡上面, 這裡使用nginx挂載
  • 思路:
  1. 首先我有一個雲伺服器, 是以有公網ip, 是以我可以實作我的html在全網可以通路, 是以拿到了我做好的html頁面即可;
  2. 由于每個伺服器的預設web服務端口是​

    ​80​

    ​​, 這裡考慮到還有很多頁面或者服務也會用到預設端口​

    ​80​

    ​​; 為啥​

    ​80​

    ​​這麼重要, 因為隻有挂載到​

    ​80​

    ​​端口的服務, 在浏覽器上隻需要輸入​

    ​域名/請求資源​

    ​​, 而不是使用​

    ​域名:指定端口/請求資源​

    ​; 也就是少了一個端口指定的操作, 畢竟端口都不好記
  3. 基于上面的問題, 想到​

    ​nginx​

    ​​, 反向代理器, 其中一個功能就是可以​

    ​把外面的請求轉化為内部的請求​

    ​​; 通俗的講, 就是當我本地服務有一個 ​

    ​ip:8001/index.html​

    ​​ 和​

    ​ip:8002/a.jpg​

    ​​, 由于上面兩個服務占用的不同的端口, 這時候我希望通過​

    ​ip/index.html​

    ​​ 和 ​

    ​ip/a.jpg​

    ​​來實作通路就需要用到nginx了, 因為他可以在外部請求(​

    ​ip/index.html​

    ​​ 和 ​

    ​ip/a.jpg​

    ​​)時轉化為内部的( ​

    ​ip:8001/index.html​

    ​​ 和​

    ​ip:8002/a.jpg​

    ​)不同服務
  4. 最終的結果就是要實作 通路​

    ​ip/請求資源​

    ​​獲得上面​

    ​sphinx-doc​

    ​​生成的​

    ​index.html​

    ​頁面
  • nginx版本: ​

    ​nginx version: nginx/1.14.0 (Ubuntu)​

  • ​​想要了解和使用sphinx-doc請點選跳轉​​

1. 安裝nginx

​$ sudo apt install nginx​

2. 設定靜态頁面請求

sudo vi /etc/nginx/nginx.conf # 配置檔案

# 下面是配置資訊
# user www-data;
user ubuntu; # 修改: 由于我的使用者是`ubuntu`, 是以這裡修改為ubuntu
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

# ------其他都不用動, 隻需要修改這裡即可-------------
server {
    listen       80;                                                         
    server_name  1.14.76.177;                                               
    # client_max_body_size 1024M;
    # 我想要請求的就是"/home/ubuntu/zjq/sphinx_doc/build/html/index.html"
    location / { # 資源路徑塊,定位資源檔案位置。
        root   "/home/ubuntu/zjq/sphinx_doc/build/html";
        index  index.html;
    }
    # 上面location 後面的 "/" 指明root/index.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/;
    }
}

# -------到這裡為止------------------------
}



sudo systemctl reload nginx # 重新加載nginx, 就可以在浏覽器上通路了      
使用nginx配置靜态頁面展示sphinx-doc

3. 遇到問題的分析

1. 網頁 403 Forbidden

使用nginx配置靜态頁面展示sphinx-doc

這是由于使用者問題, 由于​

​/etc/nginx/nginx.conf # 配置檔案​

​​中, 預設的使用者是www-data, 而這個使用者是假的, 是以需要根據目前Ubuntu的使用者進行修改, 我的是 ​

​ubuntu​

​, 修改完就好了

  • 檢視目前nginx的使用者 ​

    ​ps aux | grep "nginx: worker process" | awk '{print $1}'​

2. 網頁 404 Not Found

使用nginx配置靜态頁面展示sphinx-doc

這是由于​

​/etc/nginx/nginx.conf # 配置檔案​

​中,

  • 檢視80端口 ​

    ​netstat -ntpl | grep 80​

繼續閱讀