1 安装:
	
		
			
				 
			
				
					
						| 
							 
								1 
						 | 
						
							
								
									sudo apt-get install nginx 
							 
						 | 
					
				
			
		 
	 
	
		2 启动服务:
	
	
		或者
	
		
			
				 
			
				
					
						| 
							 
								1 
						 | 
						
							
								
									sudo /etc/init.d/nginx start 
							 
						 | 
					
				
			
		 
	 
	
		nginx默认设置了80端口的转发,启动后可以在浏览器访问http://localhost  检查是否启动成功。
	
		 
	
		3 配置
	
		默认配置文件:/etc/nginx/nginx.conf
	
		该配置文件中有两行,是用来加载外部的配置文件,如下:
	
		
			
				 
			
				
					
						| 
							 
								1 
							
								2 
						 | 
						
							
								
									include /etc/nginx/conf.d/*.conf; 
								
									include /etc/nginx/sites-enabled/*; 
							 
						 | 
					
				
			
		 
	 
	
		其中第二行的 /etc/nginx/sites-enabled/ 下有一个 default 文件,nginx的默认代理配置就在这里面。内容如下图:
	
		
	
		精简后如下:
	
		
			
				 
			
				
					
						| 
							 
								1 
							
								2 
							
								3 
							
								4 
							
								5 
							
								6 
							
								7 
							
								8 
							
								9 
							
								10 
							
								11 
							
								12 
						 | 
						
							
								
									server { 
								
									    listen 80 default_server; 
								
									    listen [::]:80 default_server;    
								
									    root /var/www/html;    
								
									    index index.html index.htm index.nginx-debian.html; 
								
									    server_name localhost; 
								
									    location / { 
								
									        # First attempt to serve request as file, then 
								
									        # as directory, then fall back to displaying a 404. 
								
									        try_files $uri $uri/ =404; 
								
									    } 
								
									} 
							 
						 | 
					
				
			
		 
	 
	
		在 /etc/nginx/conf.d/ 路径下新建一个自己项目的nginx配置文件:myproject.conf, 名字可以随便取,然后将精简后的这部分代码复制过来,根据自己的项目做相应配置,如下:
	
		
			
				 
			
				
					
						| 
							 
								1 
							
								2 
							
								3 
							
								4 
							
								5 
							
								6 
							
								7 
							
								8 
							
								9 
							
								10 
							
								11 
							
								12 
							
								13 
							
								14 
							
								15 
							
								16 
							
								17 
							
								18 
							
								19 
							
								20 
							
								21 
						 | 
						
							
								
									server 
								
									    { 
								
									        listen 80; 
								
									         access_log  /var/log/nginx/myproject.log; 
								
									         error_log   /var/log/nginx/myproject.log; 
								
									        proxy_ignore_client_abort on; 
								
									        charset utf-8;     
								
									        location ~^\/upload\/* { 
								
									                root /var; 
								
									                expires 30d; 
								
									            } 
								
									  
								
									        location / 
								
									           {    
								
									            proxy_cookie_path /myproject/ /; 
								
									                    proxy_set_header Host $host; 
								
									                    proxy_set_header X-Real-Ip $remote_addr; 
								
									                    proxy_set_header X-Forwarded-For $remote_addr; 
								
									                    proxy_pass http://localhost:8080/; 
								
									              }    
								
									    } 
							 
						 | 
					
				
			
		 
	 
	
		主要配置参数含义如下:
	
		 
	
		4 修改配置后reload:
	
	
		 
	
		参考:
	
		http://oilbeater.com/nginx/2014/12/29/nginx-conf-from-zero.html
	
		http://www.cyberciti.biz/faq/nginx-restart-ubuntu-linux-command/
		
		 
 
      (责任编辑:IT)