nginx

96 阅读1分钟

一、如何在window使用nginx来模拟服务器?

[nginx官网下载](nginx: download) 这里我选择的是稳定版本1.28.0

image.png

下载后,直接解压,双击nginx.exe运行即可,浏览器访问localhost可以看到欢迎页面。

image.png

欢迎页面其实就是与nginx.exe同级目录html/index.html这个文件。同理,你可以将自己的静态文件丢到这个目录下面访问。

二、修改配置

打开conf/nginx.conf. 主要关注http/server/location.

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

    # ----------------------自定义部分start-----------------------
    # 引入的nginx配置文件,可以将server放在该目录下,方便管理
    include ../customconf/*.conf;
    # 允许客户端上传文件最大不超过1M
    client_max_body_size 1m;
    # -----------------------自定义部分end------------------------

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

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

}

以上,除了自定义部分(已注释说明),其余即为默认配置。 server:监听端口。可以有多个. 例如

    #引入的nginx配置文件,可以将server放在该目录下,方便管理
    include ../customconf/*.conf;
server {
    listen 81;
    server_name  localhost;

    location / {
        root   html/xxx/h5;
        index  index.html index.htm;
        add_header Cache-Control "public, max-age=604800";
    }
    #  代理
    location /xxx {
        proxy_pass https://example;
        proxy_set_header Host $host; 
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /xx1 {
        proxy_pass https://example;
        proxy_set_header Host $host; 
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

访问时需要加上端口, 即http://localhost:81, 这种情况比较少,一般都是默认80端口下部署多个项目, 需要在同一个Server下新增location。

路由匹配规则:

location匹配: 参考文章

root和alias区别.

location [修饰符] /demo { 
    # root: /a/b
    # alias: /a/b
}

root是拼接,访问/demo, 实际是/a/b/demo alias是别名,访问/demo, 实际是/a/b

常见问题

  1. nginx -s stop 命令执行后,网页依然可以访问。

netstat -ano | findstr 80 查看占用80端口的进程的PID image.png taskkill /f /t /im nginx.exe 结束nginx进程 image.png