工具使用:Ngnix,工具使用ngnix


####1,CentOS上

安装Nginx: yum install nginx   (/etc/nginx)

卸载Nginx:yum remove nginx

查看Nginx安装目录: ps -ef | grep nginx 

启动Nginx:    service nginx start

杀掉进程:pkill -9 nginx 		    

####2,增加防火墙的访问权限:
-A INPUT -p TCP --dport 80 -j ACCEPT -A OUTPUT -p TCP --sport 80 -j ACCEPT


####3,反向代理,虚拟域名的配置 :
编辑 sudo vim /usr/local/nginx/conf/nginx.conf


####4,mac上Nginx的安装:

(1),brew install nginx

	安装完以后,可以在终端输出的信息里看到一些配置路径:
	 /usr/local/etc/nginx/nginx.conf (配置文件路径)
	 /usr/local/var/www (服务器默认路径)
	/usr/local/Cellar/nginx/1.12.0 (安装路径)(我安装的是1.12.0,具体参照自己安装的版本)

(2),启动

进入安装路径   cd  /usr/local/Cellar/nginx/1.12.0/bin
	启动 sudo ./nginx
	重启 sudo ./nginx -s reload 
	判断配置文件是否正确  sudo ./nginx -t 
	#重新加载配置|重启|停止|退出 nginx
	 nginx -s reload|reopen|stop|quit

(3),访问
localhost:8080


(4),修改nginx配置

	打开 nginx.config 文件
	 /usr/local/etc/nginx/nginx.conf
	 在该文件底部引入vhost下所有conf配置文件 
	 include vhost/*.conf 
	在vhost下创建mytest.conf
	touch mytest.conf
	重启nginx浏览器访问即可。

(5),mytest.conf具体内容:

    server {
	         listen 8000;	
	         server_name localhost;
	        	         root /Users/xxxxx/myCode/AntDesgn_Demo2/dist;
		         location /api {
			 proxy_pass http://localhost:8000;
		 }
	         location / {
	             try_files $uri $uri/ @router;
	             index index.html;
	         }
		          location @router {
	            rewrite ^.*$ /index.html last;
	         }
	    }      

(6),修改nginx监听端口为3000避免与tomcat冲突。 nginx.conf文件:

worker_processes  1;
   			 			events {
   			    worker_connections  1024; 			}
   			
   			 			http {
   			    include       mime.types;
   			    default_type  application/octet-stream;
   			
   			    sendfile        on;
   			
   			    keepalive_timeout  65;
   			    #gzip  on;
   			    server {
   			        listen       3000;
   			        server_name  localhost;
   			
   			        #charset koi8-r;
   			
   			        #access_log  logs/host.access.log  main;
   			
   			        location / {
   			            root   html;
   			            index  index.html index.htm;
   			        }
   			
   			        #error_page  404              /404.html;
   			        error_page   500 502 503 504  /50x.html;
   			        location = /50x.html {
   			            root   html;
   			        }
   			    }
   			    include servers/*;
   			    include vhost/*.conf; 			}

(7),修改nginx监听端口为3000避免与tomcat冲突。 nginx.conf.default同上(6)

(8) , nginx请求转发到tomcat配置;修改nginx.conf

	 server {
        listen       80;
        server_name  xxx.xxx.xxx.xxx:8080;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
	   root html;
	   index index.html index.htm;
	   #要转发到的地址
	   proxy_pass http://你的网址:8080;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

####5,附录:Nginx的SSL证书配置: vi /etc/nginx/conf.d/ssl.conf

 #server configuration
 

 server {
    listen       443 ssl http2 default_server;
    listen       [::]:443 ssl;
    server_name  _;
    root         指向的目录;

    ssl_certificate xxx/xxx.crt;
    ssl_certificate_key xxx/xxx.key;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

#    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

     location / {
              try_files $uri $uri/ @router;
             index index.html;
     }

     location @router {
            rewrite ^.*$ /index.html last;
         }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

image服务配置:vi /etc/nginx/vhost/image.conf

server {
             listen 8686;   
             server_name localhost;
	     
             autoindex off;

	     index index.html index.htm index.jsp index.php;
             
	     location / {
			root /home/ftpfile/;
			add_header Access-Control-Allow-Origin *;
           }
        }  
      

端口转发的配置(监听nginx的https端口443,把其中访问转发到tomcat的8443端口上去:

server {
    listen       443 ssl http2 default_server;
    listen       [::]:443 ssl;
    
    ssl_certificate XXXXXXXX.crt;
    ssl_certificate_key XXXXXXXX.key;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    include /etc/nginx/default.d/*.conf;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass https://XX.XX.XX.XX:8443;
    }

}

相关内容

    暂无相关文章