nginx环境配置Windows本地测试,测试打包后的代码_nuxt2 如何配置nginx(1)

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

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


}


![](https://img-blog.csdnimg.cn/20190510001549908.png)



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


![](https://img-blog.csdnimg.cn/20190510000226725.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNjQ2MjQ5,size_16,color_FFFFFF,t_70)


**关闭:输入:nginx -s stop**


![](https://img-blog.csdnimg.cn/20190510000308214.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNjQ2MjQ5,size_16,color_FFFFFF,t_70)



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



常用到的命令如下:


    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包以及页面路径放置如下**:


![](https://img-blog.csdnimg.cn/20190510000819193.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNjQ2MjQ5,size_16,color_FFFFFF,t_70)


 ![](https://img-blog.csdnimg.cn/20190510000857454.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNjQ2MjQ5,size_16,color_FFFFFF,t_70)



**②更改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**


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




![img](https://img-blog.csdnimg.cn/img_convert/ba1a7b1d3d4077041d61cbe8abfa8d14.png)
![img](https://img-blog.csdnimg.cn/img_convert/9907d32fa439765aa2f1304d74613e24.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/topics/618631832)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

com**


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




[外链图片转存中...(img-xBt7USoq-1715811036508)]
[外链图片转存中...(img-7lThAffI-1715811036508)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/topics/618631832)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值