Nginx在windows和Linux环境下搭建静态资源服务器

一、Nginx

下载Nginx

二、在windows下安装

在window下安装是比较简单的,只需要解压下载的Nginx就可以了。

2.1、配置文件

在配置文件中如果涉及到路径问题,需要注意的是\n这样的是会被转义的,需要写成双斜杠

# Windows下的文件夹路径
D:\work\nginx-1.17.7\upload
# 配置文件中的文件夹路径
D:\\work\\nginx-1.17.7\\upload

2.2、配置文件

2.2.1、nginx.conf


#user  administrator owner;
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;
        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;
    #    }
    #}
    include D:\\work\\nginx-1.17.7\\upload\\upload.conf;
}

2.2.2、upload.conf文件

server
{ 
  charset utf-8;
  listen 8089;
  server_name http_host;
  root D:\\work\\nginx-1.17.7\\upload;
  autoindex on;
  add_header Cache-Control "no-cache, must-revalidate";
  location / { 
    add_header Access-Control-Allow-Origin *;
  }
}

2.3、window下的坑

在windows下启动Nginx的方式有直接双击运行nginx.exe文件。但是如果想关闭,则发现有可能通过下面的命令也无法关闭的情况。

./nginx.exe -s stop

这个时候可以使用下面的杀手锏,直接kill掉所有的Nginx的服务。

taskkill  /im nginx.exe  -f

猜你喜欢

转载自blog.csdn.net/weixin_38657051/article/details/106782758
今日推荐