docker中安装的nginx,使用的是嵌套式的nginx配置,即主配置在/etc/nginx/nginx.conf中,然而平时用到的server配置在/etc/nginx/conf.d中两个配置文件如下:

nginx.conf


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
worker_connections  1024;
}


http { 
include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

}

在底部include了另外一部分配置文件,而这部分配置文件就是基本的server配置,默认模板如下:

server {
    listen       80;
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
 
    location / {
        root   /usr/nginx/dacheng-wechat-web;
       # root   /usr/nginx/html;
        index  index.html index.htm;
        autoindex  on;
	try_files $uri /index/index/page.html;
        #try_files $uri /index/map/page.html;
    }
 
    #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   /usr/share/nginx/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;
    #}
}
 

此处按照nginx配置文件中的server标准配置location配置路由以及页面路径(location / 这个/就是页面访问路径,此处为根路径。location / {}大括号内配置详细页面路径,root后面跟页面目录,index后面跟默认显示页面)。

此处要特别注意,页面目录不能使用自己主机的目录而要使用docker安装的nginx下的目录配置,因为这个配置文件最终会被docker容器解析给nginx所以应该配置docker的nginx容器下的路径,然后再使用docker容器构建时候的目录映射功能将docker下的nginx目录映射到自己主机上操作。

因为此处只做示例作用就没有将nginx.conf映射出来,容器构建代码如下:

docker run 
-p 80:80 
--name mynginx 
-v D:\Docker\nginx\www:/www 
-v D:\Docker\nginx\conf.d:/etc/nginx/conf.d 
-v D:\Docker\nginx\wwwlogs:/wwwlogs 
-d nginx

构建之后的conf.d文件夹中存储server配置信息,即:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /www/dist/stategrid;
        index  index.html;
    }

    #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   /usr/share/nginx/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;
    #}
}

此处location配置root页面路径时前面/www为docker内路径,而因为映射到了外部主机上,所以通过/www即可访问外部主机资源/dist/stategrid(docker内路径/www,自己主机资源存储路径D:\Docker\nginx\www\dist\stategrid)

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐