寫給Android開發的Nginx入門


介紹

  1. 高性能的web服務器,開源免費
  2. 一般用於做靜態服務,負載均衡
  3. 用於反向代理 

安裝

Mac使用homeBrow安裝,Centos使用yum安裝

常用命令

# 重啟nginx
nginx -s reload
# 停止nginx
nginx -s stop
# 測試配置文件是否正確,同時會列出配置文件位置
nginx -t

Nginx配置

# 使用 nginx -t 來顯示conf文件位置
http{
        server{
                listen  8080;    # 監聽端口
                server_name     localhost; # _相當於0.0.0.0;也可以指定域名
                # 配置靜態文件
                location / {
                    root /User/me/html;    # /路由尋找的目錄
                    index index.html;        # index文件名
                }
                # 配置動態轉發
                location /home {     # 轉發
                        proxy_pass              http://localhost:8001;
                }
                location /api/ {    # 轉發
                        proxy_pass              http://localhost:8000;
                        proxy_set_header        Host $host;
                }
        }
}

快速啟動動態服務

用途:
  1. 和前端聯調時,登錄功能依賴cookie,必須使用瀏覽器聯調
  2. cookie跨域不共享,前端和server必須同域
  3. 需要用nignx做代理,讓前后端同域 

使用node的http服務

# 需要sudo權限;-g是global全局安裝
npm install http-server -g
# 啟動http服務,指定端口,默認是8080
http-server -p 8001

雲服務器配置https

  1. 申請騰訊雲證書
  2. 下載並上傳到服務器,解壓:目錄中有Apache、Nginx、Tomcat等文件夾,使用Nginx文件夾中的兩個文件,crt文件是證書,key文件是秘鑰
  3. 配置conf:conf文件里有http部分和https部分,默認是打開http部分的,現在注釋掉http部分,打開https部分,並指定crt和key文件以及域名即可
  4. 瀏覽器訪問https://<host> 即可 
    server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  xxx.xx yyy.yy; # 指定申請的域名,若多個域名使用空格隔開
        root         /usr/share/nginx/html;

        ssl_certificate "/etc/pki/nginx/server.crt”; # 復制crt文件
        ssl_certificate_key "/etc/pki/nginx/private/server.key”; # 復制key文件
        ssl_session_cache shared:SSL:1m; 
        ssl_session_timeout  10m;
        ssl_ciphers PROFILE=SYSTEM;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM