Nginx环境配置Windows本地测试

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

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

启动方式:

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

    

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

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

    

    关闭:输入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

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

关于nginx更详细的资料见另一篇《nginx相关》

背景:

    想起在本地配nginx的主要原因是访问系统外网不能获取客户真实的IP,开始先把远程的nginx关掉,直接访问系统,没能解决问题,第一步排除是由于nginx导致IP获取不准确;

    在本地服务器访问内网系统,并用postmen测试能够准确获取IP;

    在本地配置nginx能够正确获取IP,进一步排除由于nginx导致IP获取不准确;

最后还是没能解决IP的获取问题,与同事交流认为是远程服务器那边的网络环境改变了,我对于网络这方面不太了解,具体如何解决这个问题还有待与相关专业人士学习,路漫漫其修远兮。

猜你喜欢

转载自blog.csdn.net/gxl_1012/article/details/80793182
今日推荐