安装Nginx1.15.7并配置和Docker搭建nginx

转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/85225075 谢谢

下载其他需要的工具

yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel

安装Nginx

创建文件夹

mkdir -p /usr/local/nginx

下载Nginx

cd /usr/local/nginx
wget https://nginx.org/download/nginx-1.15.7.tar.gz

解压

tar -zxvf nginx-1.15.7.tar.gz

configure配置

cd /usr/local/nginx/nginx-1.15.7
./configure --prefix=/usr/local/nginx

编译

make && make install

放行端口号

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

启动nginx

./usr/local/nginx/sbin/nginx
  • 查看状态
ps -ef|grep nginx
  • 杀死进程
kill -9 nginx
  • 验证nginx配置文件是否正确
./usr/local/nginx/sbin/nginx -t
  • 重启Nginx
./nginx -s reload
  • 停止Nginx
    • 强制
    ./nginx -s stop
    
    • 完整
    ./nginx -s quit
    

在这里插入图片描述

配置Nginx

  • 配置详解
#user  nobody;

#开启进程数 <=CPU数  
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 {
#单个进程最大连接数(并发量)(总并发=单进程并发量数x进程数),每个worker允许同时产生多少个链接.
    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压缩  
    #gzip  on;
    #http的协议版本
	#gzip_http_version 1.0;    
	#如果是IE的话,就关闭压缩
	#gzip_disable 'MSIE [1-6].';
	#需要压缩的文件格式
	#gzip_types image/jepg;


	# 一个网站虚拟机
    server {
     	 #端口,默认是80端口 
        listen       80;
        #域名 
        server_name  localhost;
		 #网站根目录
		 #root /usr/local/nginx/html
		 
        #charset koi8-r;
 		 #nginx访问日志,使用main格式(可以自定义格式)
        #access_log  logs/host.access.log  main;
        
        location / {
            #root指定nginx的根目录为/usr/local/nginx/html  
            root html;  
            #默认访问文件,欢迎页先去html目录下找index.html,如果找不到再去找index.htm  
            index index.html index.htm;  
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #错误页面及其返回地址,错误码为500、502、503、504都会返回50.html  
        error_page 500 502 503 504 /50x.html;  
        #location后面是"=",则是精确匹配
        location = /50x.html {
            root   html;
        }
		#缓存图片文件
		# location ^.*\[jgp|png] { #缓存图片类型:jgp|png等
    	#缓存时间1天 d:天 h:小时
     	#	expired 1d;
 		#}
  
        # 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;
    #    }
    #}

}

路由配置(负载均衡)

  • 先准备好web项目,我这里就用tomcat代替了 tomcat安装

配置

	upstream www.tomcat.com {
		 server 192.168.0.109:8080 weight=1; #权重,值越大,被访问到的机会就越大.
		 server 192.168.0.110:8080 weight=2;
		 #ip_hash; #负载均衡算法:ip_hash算法,默认使用轮询算法.
		 #server 192.168.0.109:8080 backup; #设为备机,服务并发特别大的时候,才会生效.
	  }

		 location / {
			proxy_pass http://www.tomcat.com; # http协议开头
		}
  • 完整
#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;

	upstream www.tomcat.com {
		 server 192.168.0.109:8080 weight=1; #权重,值越大,被访问到的机会就越大.
		 server 192.168.0.110:8080 weight=2;
		 #ip_hash; #负载均衡算法:ip_hash算法,默认使用轮询算法.
		 #server 192.168.0.109:8080 backup; #设为备机,服务并发特别大的时候,才会生效.
	  }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		
		 location / {
			proxy_pass http://www.tomcat.com; # http协议开头
		}
		
        #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;
    #    }
    #}

}
  • 重启Nginx并刷新配置
./usr/local/nginx/sbin/nginx -s reload
  • 再次访问nginx
http://192.168.0.109/

在这里插入图片描述

动静分离

创建文件夹

mkdir -p /usr/local/static/jcs

配置

		location ~.*\.(js|css)$ {
			root /usr/local/static/jcs;
			expires 1d;
		}
		location ~.*\.(jpg|img|jepg|wmv|wma|text|avi|mp3|mp4|flv)$ {
			root /home/myftp;
			expires 10d;
		}
  • 完整

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

	#upstream www.tomcat.com {
	#	server 192.168.0.109:8080 weight=1; #权重,值越大,被访问到的机会就越大.
	#	server 192.168.0.110:8080 weight=2;
	#	 #ip_hash; #负载均衡算法:ip_hash算法,默认使用轮询算法.
	#	 #server 192.168.0.109:8080 backup; #设为备机,服务并发特别大的时候,才会生效.
	#  }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		
		# location / {
		#	proxy_pass http://www.tomcat.com; # http协议开头
		#}
		
        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}
		
		location ~.*\.(js|css)$ {
			root /usr/local/static/jcs;
			expires 1d;
		}
		location ~.*\.(jpg|img|jepg|wmv|wma|text|avi|mp3|mp4|flv)$ {
			root /home/myftp;
			expires 10d;
		}
		
        #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;
    #    }
    #}

}

  • 重启Nginx并刷新配置
./usr/local/nginx/sbin/nginx -s reload
  • 访问
    • 如果访问ftp出现403 Forbidden,请授权chmod 755 /home/myftp

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

Docker搭建

拉取镜像

docker pull nginx

创建文件夹

新建资源文件夹

mkdir -p /usr/local/docker-nginx/html
chmod 777 /usr/local/docker-nginx/html

新建配置文件夹

mkdir -p /usr/local/docker-nginx/conf.d
mkdir -p /usr/local/docker-nginx/conf
mkdir -p /usr/local/docker-nginx/log
chmod 777 /usr/local/docker-nginx/conf.d
chmod 777 /usr/local/docker-nginx/conf
chmod 777 /usr/local/docker-nginx/log

配置文件 nginx.conf

vim /usr/local/docker-nginx/conf/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;
}

配置文件 default.conf

vim /usr/local/docker-nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

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

    location / {
        root   /usr/share/nginx/html;
        index index.html 6666.jpg;
    }

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

放行端口号

firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --reload

运行容器

  • 日志文件位置:/var/log/nginx
  • 配置文件位置: /etc/nginx
  • 资源存放的位置: /usr/share/nginx/html
docker run --name nginx -p 8081:80 -v /usr/local/docker-nginx/html:/usr/share/nginx/html -v /usr/local/docker-nginx/log:/var/log/nginx  -v /usr/local/docker-nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /usr/local/docker-nginx/conf.d:/etc/nginx/conf.d --privileged=true -d nginx
  • 我在html文件夹下放了一张照片,于是访问http://192.168.0.131:8081/
    在这里插入图片描述
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值