Windows 環境配置 Nginx


部署

第一步

  從官網下載 Nginx 包,版本自己選擇,官網地址:http://nginx.org/

 

第二步

  選擇 Windows 版本的安裝包進行下載,根據圖片描述,最新版是 1.19.3 (具體看當前官網),穩定版是 1.18.0 (具體看當前官網)

 

第三步

  下載后得到的 exe 文件不要雙擊,用解壓程序進行解壓,得到一個文件夾,將文件夾復制到指定目錄就完成了安裝

注意:一定不要直接雙擊nginx.exe,這樣會導致修改配置后重啟、停止 nginx 無效,需要手動關閉任務管理器內的所有 nginx 進程,再啟動才可以

 

第四步

  啟動 Nginx

# 進入 Nginx 所在目錄
cd c:\nginx-1.xx.x

# 啟動 Nginx 服務
start nginx  # 執行后會有一閃而過的信息,是正常的

# 查看任務進程是否存在,可使用以下命令s或打開任務管理器都行
tasklist /fi "imagename eq nginx.exe"

# 日志路徑 c:\nginx-1.xx.x\log

 

第五步

  瀏覽器訪問 127.0.0.1:80,查看是否成功,看到以下頁面表示成功

 

 

 

 

文檔結構

 

默認配置文件路徑 -- > c:/nginx-1.xx.x/conf/nginx.conf

#user  nobody;
# ---------- 工作進程數,一般設置為cpu核心數
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

# ---------- 最大連接數,一般設置為cpu*2048
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    # ---------- 客戶端鏈接超時時間
    keepalive_timeout  65;

    #gzip  on;

    # ---------- 當配置多個server節點時,默認server names的緩存區大小就不夠了,需要手動設置大一點
    server_names_hash_bucket_size 512

    # ---------- server表示虛擬主機可以理解為一個站點,可以配置多個server節點搭建多個站點,每一個請求進來確定使用哪個server由server_name確定
    server {
        listen       80;
        server_name  localhost;

        # ---------- 編碼格式,避免url參數亂碼可修改為charset utf-8
        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        # ----------location用來匹配同一域名下多個URI的訪問規則,比如動態資源如何跳轉,靜態資源如何跳轉等。location后面跟着的/代表匹配規則
        location / {  # 站點根目錄,可以是相對路徑,也可以使絕對路徑
            root   html;
            index  index.html index.htm;  # 默認主頁
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;  # 轉發后端站點地址,一般用於做軟負載,輪詢后端服務器
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
nginx.conf

 

注意事項

  1. Nginx 默認端口號為 80,若已被占用則無法啟動,可在配置文件中修改端口號,修改陪之后可執行命令檢查配置是否合法
    nginx -t -c /nginx-1.xx.x/conf/nginx.conf
  2. Nginx 文件夾路徑不要包含中文

 

其他

  NGINX 相關命令

                   

 

 

 

 

             


免責聲明!

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



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