window nginx 簡單搭建服務器訪問靜態資源


nginx命令:

啟動: start nginx

停止:nginx -s stop ||  nginx -s quit

    注:stop是快速停止nginx,可能並不保存相關信息;quit是完整有序的停止nginx,並保存相關信息。

查看版本:nginx -v

重載配置:nginx -s reload

檢查語法:nginx -t 

步驟:

一:安裝window (我目前使用的系統是win7)

 官網下載地址: https://nginx.org/en/download.html 如下圖點擊windows下載解壓:(我安裝版本:nginx-1.14.2)

解壓之后的文件目錄:

二:運行nginx

  在當前目錄中打開cmd,快捷鍵方式:ctrl+shift -->鼠標右鍵---》在此處打開命令窗口

  啟動nginx: start nginx

  在瀏覽地址欄輸入:localhost 就會啟動nginx默認html頁面  ||  在cmd輸入ipconfig獲取當前使用電腦的ip地址

如上圖啟動成功;

三:配置自己個服務端口

  打開:nginx-1.14.2/conf/nginx.conf

#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;


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 {
        listen 9000;//我修改過,防止和我啟動其他服務發生沖突 這樣我在啟動nginx,打開默認頁面url:localhost:9000
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        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;
        #}
    }
    server { #這里是我自己配置服務端口 listen 8081;
        server_name resouce;
        root  D:/book/resouces;  #訪問文件根目錄
        autoindex on;  #是否瀏覽文件下的列表
        location / {  #是否允許跨域
            add_header Access-Control-Allow-Origin *;
        }
        add_header Cache-Control "no-cache,must-revalidate";# 是否緩存 }

    # 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;
    #    }
    #}

}

上面listen:8081就是我自己配置服務器端口,只要啟動nginx 我就可以通過localhost:8081訪問我resouce目錄下的文件

這樣簡單的,nginx 靜態資源服務器就搭建成功了;

以下是我遇到的問題:

 ①修改完配置之后,我nginx -s reload報錯:

[錯誤]|nginx: [error] invalid PID number "" in "/usr/local/nginx-1.12.2/nginx_my.pid"

  解決方案:檢查nginx是否在啟動(打開頁面查看是否可以打開 || 使用命令 nginx -s reopen || 查看電腦進程是否存在 nginx 等等),確定啟動之后再nginx -s reload;

  原因:nginx -s reload 僅告訴正在運行的nginx進程重新加載其配置,停止之后,沒有正在運行的nginx進程發送信號,才會報錯,找不到nginx_my.pid中的PID

  參考頁面:https://blog.csdn.net/qq_28296925/article/details/80416552

 

 比較簡單,只是作為一個簡單的小記。

 


免責聲明!

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



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