nginx环境配置Windows本地测试,测试打包后的代码

一、nginx环境配置Windows本地测试

Windows版和Linux版下载地址:http://nginx.org/en/download.html

本文只讲Windows系统的

下载后解压到指定目录即可

启动方式:

 1、启动:进入主目录下,双击nginx.exe

 2.关闭:启动任务管理器,找到Nginx,结束进程

启动的步骤:

方法①:点nginx.exe 右键以管理员的身份运行, 关闭得在资源管理器里面找到nginx ,关掉进程。

把打包后文件丢在:html文件夹里面

 

修改nginx的配置文件:在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;
            try_files $uri $uri/ /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   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;
    #    }
    #}

}

 我这配置是本地访问localhost,在浏览器直接输入localhost 就可以访问了,

但是,只要你刷新浏览器就会报404;

原因在于少一句代码:

location / {

try_files $uri $uri/ /index.html;

}

方法②:启动:在主目录下打开DOS命令行窗口,输入: start nginx

 关闭:输入:nginx -s stop

推荐使用第二种方法,第一种方法关闭的时候太麻烦

常用到的命令如下:

    nginx -s stop       快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
    nginx -s quit       平稳关闭Nginx,保存相关信息,有安排的结束web服务。
    nginx -s reload     因改变了Nginx相关配置,需要重新加载配置而重载。
    nginx -s reopen     重新打开日志文件。
    nginx -c filename   为 Nginx 指定一个配置文件,来代替缺省的。
    nginx -t            不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
    nginx -v            显示 nginx 的版本。

    nginx -V            显示 nginx 的版本,编译器版本和配置参数。

另外,如果想每次都手动敲命令开启和关闭nginx,可以在nginx安装目录下新添一个启动批处理文件startup.bat,双击即可运行。内容如下:


 

@echo off
rem 如果启动前已经启动nginx并记录下pid文件,会kill指定进程
nginx.exe -s stop
 
rem 测试配置文件语法正确性
nginx.exe -t -c conf/nginx.conf
 
rem 显示版本信息
nginx.exe -v
 
rem 按照指定配置去启动nginx
nginx.exe -c conf/nginx.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下进行单独配置(需要代理的server)
	  client_max_body_size 10m;  #允许客户端请求的最大单文件字节数 
	  client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
      proxy_redirect off;
      proxy_set_header Host $host;#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
      proxy_set_header X-Real-IP $remote_addr; # 真实的客户端IP
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 代理路由信息,此处取IP有安全隐患
	  proxy_set_header   X-Forwarded-Proto $scheme;# 真实的用户访问协议
	  proxy_connect_timeout 65;#nginx跟后端服务器连接超时时间(代理连接超时)
      proxy_send_timeout 65; 
      proxy_read_timeout 65;#连接成功后,后端服务器响应时间(代理接收超时)
      proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
      proxy_buffers 4 32k;#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
      proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
	  proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
	  
	#负载均衡组
	#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
	#静态服务器组 
	upstream staticCluster{
            server 192.168.0.144:8088 weight=1;
    }
	#动态服务器组
	upstream dynamicCluster{
			#ip_hash;
			server  192.168.0.144:8088 weight=1;
			#server  192.168.0.144:8089 weight=1;
    }
 
     server {
		#监听端口
        listen       80;
		#域名可以有多个,用空格隔开
        server_name  localhost;
		
        #charset koi8-r;
		
		#设置编码为utf-8;
		charset    utf-8;
		
		#nginx访问日志
        #access_log  logs/host.access.log  main;
 
        #location / {            
        #    index  login.html;
        #}
		#页面地址
		set $dz /dev/opt/web/demo;
		
		location = / {
				root $dz;
				index  login.html;
		}	
		
		#静态文件交给nginx处理
		location ~* \.(htm|html|json|gif|jpg|jpeg|png|bmp|swf|ico|rar|zip|txt|flv|mid|doc|docx|ppt|pdf|xls|xlsx|mp3|wma|eot|svg|ttf|woff)$ {
				#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移
				#proxy_next_upstream http_502 http_504 error timeout invalid_header;
				#proxy_cache cache_one;
				
				#对不同的HTTP状态码设置不同的缓存时间
				proxy_cache_valid 200 304 302 5d;
				#proxy_cache_valid any 5d;
				
				#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
				proxy_cache_key '$host:$server_port$request_uri';
				
				add_header X-Cache '$upstream_cache_status from $host';
				#静态服务器组
				#proxy_pass http://staticCluster;
					
				#所有静态文件直接读取硬盘
                root $dz;
                expires 30d; #使用expires缓存模块,缓存到客户端30天
            }
		
		#静态文件交给nginx处理
		location ~* \.(js|css)$ {
					root $dz;
					expires 1h;
				}
 
		#动态文件交给tomcat/jetty处理
		 location ~*$ {
		 
					 proxy_intercept_errors on; # 关键参数:这个变量开启后,我们才能自定义错误页面,当后端返回404,nginx拦截错误定义错误页面
                     #proxy_pass http://127.0.0.1:8088;
					 proxy_pass http://dynamicCluster;
           }
		#fastcgi_intercept_errors on; 
		# 关键参数:这个变量开启后,我们才能自定义错误页面,当后端返回404,nginx拦截错误定义错误页面 
        error_page  404   /login.html;
		location = /login.html {
				root $dz;
				index  login.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、jar包以及页面路径放置如下

 

②更改host:在C:\Windows\System32\drivers\etc目录下的host文件中添加一条DNS记录

127.0.0.1 www.demo.com

③运行jar包,双击也行,在jar包所在的目录下打开DOS窗口输入:java -jar demo.jar也行。

④启动nginx,根据上面的配置现在输入www.demo.com

这时候应该就可以访问了。
 

最后为了方便大家的沟通与交流请加QQ群: 625787746

请进QQ群交流:【IT博客技术分享群①】:https://jq.qq.com/?_wv=1027&k=DceI0140

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT博客技术分享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值