如何在Windows环境配置 Nginx?

目录
文章目录隐藏
  1. 部署
  2. 文档结构
  3. 注意事项
  4. NGINX 相关命令​

部署

1、首先,从官网下载 Nginx 包,版本自己选择,官网地址:传送门

Nginx 版本下载2、然后,选择 Windows 版本的安装包进行下载(大家根据自己实际情况下载对应版本),根据图片描述,最新版是 1.23.1 (具体看当前官网),稳定版是 1.22.0 (具体看当前官网):

选择 Windows 版本的安装包

3、我们选择合适的版本下载到本地后,用解压程序进行解压,得到一个文件夹,将文件夹复制到指定目录就完成了安装,解压后得到的 .exe 文件不要双击!

​注意:一定不要直接双击 nginx.exe,这样会导致修改配置后重启、停止 nginx 无效,需要手动关闭任务管理器内的所有 nginx 进程,再启动才可以。

4、启动 Nginx

# 进入 Nginx 所在目录
cd D:\nginx-1.xx.x

# 启动 Nginx 服务
start nginx  # 执行后会有一闪而过的信息,是正常的

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

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

5、到浏览器访问 127.0.0.1:80,查看是否成功,看到以下页面表示成功:

Nginx 安装页面成功

文档结构

文档结构

默认配置文件路径 D:/nginx-1.xx.x/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       80;
        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;
        #}
    }

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

}

注意事项

  1. Nginx 默认端口号为 80,若已被占用则无法启动,可在配置文件中修改端口号,修改配置后可执行命令检查配置是否合法。
  2. Nginx 文件夹路径不要包含中文。

NGINX 相关命令​

「点点赞赏,手留余香」

1

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » 如何在Windows环境配置 Nginx?

发表回复