一文带你搞懂Nginx,老王出品,必属精品

前言:Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务:

一、nginx 基础常用命令:

1. ./nginx -t #检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
2. ./nginx -c /home/xx/nginx.conf #指定一个配置文件,来代替缺省的。
3. ./nginx -v #nginx 的版本。
4. ./nginx -s reload #reload 会重新加载配置文件,Nginx服务不会中断。而且reload时会测试conf语法等。
5. ./nginx #启动nginx。
6. ./nginx -s stop #stop 会立即停止服务,这种方法比较强硬,无论进程是否在工作,都直接停止进程。
7. ./nginx -s quit #quit 较stop相比就比较温和一些了,需要进程完成当前工作后再停止。

二、nginx 配置反向代理转发proxy_pass:

#=======================================location 带/结尾=======================================
# 请求url http://127.0.0.1:8080/proxy/index.html
location /proxy/ {
    proxy_pass http://127.0.0.1:8080/;
}
# 代理地址以 "/" 结尾,代理转发的url地址为:http://127.0.0.1:8080/index.html

location /proxy/ {
    proxy_pass http://127.0.0.1:8080;
}
# 代理地址不以 "/" 结尾,代理转发的url地址为:http://127.0.0.1:8080/proxy/index.html

location /proxy/ {
    proxy_pass http://127.0.0.1:8080/tomcat/;
}
# 代理地址以 "tomcat/" 结尾,代理转发的url地址为:http://127.0.0.1:8080/tomat/index.html

location /proxy/ {
    proxy_pass http://127.0.0.1:8080/tomcat;
}
# 代理地址以 "tomcat" 代理转发的url地址为:http://127.0.0.1:8080/proxytomcat/index.html

#=======================================location 不带/结尾=======================================
location /proxy {
    proxy_pass http://127.0.0.1:8080/tomcat;
}
# 代理地址以 "tomcat" 代理转发的url地址为:http://127.0.0.1:8080/tomcat/index.html

location /proxy {
    proxy_pass http://127.0.0.1:8080/;
}
# 代理地址以 "/" 代理转发的url地址为:http://127.0.0.1:8080//index.html

location /proxy {
    proxy_pass http://127.0.0.1:8080;
}
# 代理地址不以 "/" 代理转发的url地址为:http://127.0.0.1:8080/proxy/index.html

#=======================================alias与root=======================================
location /test/ {
	alias /www/abc/;
}
# 使用alias,当访问/test/时,会到/www/abc/目录下找文件

location /test/ {
	root /www/abc;
}
# 使用root,当访问/test/时,会到/www/abc/test/目录下找文件(如果没有test目录会报403)

#================================多层nginx代理@Websocket服务=======================================
location /secure/socket {
	add_header backendIP $upstream_addr;
	add_header backendCode $upstream_status;
	proxy_redirect off;
	proxy_connect_timeout 6000;
	proxy_read_timeout 6000; 
	proxy_send_timeout 6000;
	proxy_set_header Host 192.168.9.101:8087;
	proxy_pass http://localhost:8080/websocket/web;
	proxy_http_version 1.1;
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection "upgrade"; 
	proxy_set_header token $arg_username; 
}
#================================多层nginx代理@Java服务=======================================
#主机127.0.0.1下的nginx配置,port=8081
location /local/app/ {
    proxy_pass http://10.9.103.36:8081/;
}
#主机10.9.103.36下的nginx配置,port=8081
location /chat/ {
    proxy_pass http://10.186.253.117:8081/;
}
#================================多层nginx代理@HTML资源=======================================
#主机10.9.103.35下的nginx配置,port=8083
location ^~ /html/chat/ {
    #符号^~:一旦匹配到,就不继续匹配(静态资源匹配)
	proxy_pass http://10.9.103.36:8081/;
}

#主机10.9.103.36下的nginx配置,port=8081(下图为html资源目录结构)
location /mystatic {
    root html;
    index index.html index.htm;
}

多层代理-10.9.103.36:8081的目录结构

 //真实提供服务的为10.186.253.117:8081这个服务========多层nginx代理@Java服务
 String url = "http://127.0.0.1:8081/local/app/chat/LargeModelLayout/session/sso";
 HashMap<String, Object> requestBody = new HashMap<>(1);
 requestBody.put("app","kuandai");
 Map<String, Object> objectMap = HttpUtils.doJsonGet(url, new HashMap<>(0), requestBody);
 //Java多层代理分析:
 //http://127.0.0.1:8081/local/app 匹配到第一层nginx代理转发到http://10.9.103.36:8081/;
 //http://10.9.103.36:8081/chat 匹配到第二层nginx代理转发到http://10.186.253.117:8081/;
 //最终格式url为:http://10.186.253.117:8081/LargeModelLayout/session/sso
 //由http://10.186.253.117:8081/LargeModelLayout/session/sso提供服务

//真实HTML资源由10.9.103.36:8083提供服务========多层nginx代理@HTML资源
 String url = "http://10.9.103.35:8083/html/chat/mystatic/";
 //HTML多层代理分析:
 //http://10.9.103.35:8083/html/chat/ 匹配到第一层nginx代理转发到http://10.9.103.36:8081/
 //http://10.9.103.36:8081/mystatic/ 匹配到第二次nginx静态资源目录(html/mystatic)
 //最终格式url为:http://10.9.103.36:8081/mystatic/index.html
 //请注意上文静态资源配置的注意事项!!!

三、nginx 配置负载均衡策略(默认为轮询策略,支持: 轮循 Round Robin、加权轮循 Weighted Round Robin、最少连接数 Least Connection、源 IP 哈希 Source IP Hash等多种策略):

upstream webservers{
	server  127.0.0.1:8080;
	server  127.0.0.1:8081;
	server  127.0.0.1:8082;
}

location / {
	#转发到负载服务上
 	proxy_pass http://webservers;
}

四、openssl生成证书

1. openssl生成ssl证书:

1.1. 下载opens ssl并安装:http://slproweb.com/products/Win32OpenSSL.html 官网地址
1.2. 安装openssl且配置环境变量,新增系统变量:变量名 OPENSSL_HOME 变量值 D:\Program Files\OpenSSL-Win64\bin(以自己实际安装路径为准)

在这里插入图片描述

1.3. 在path变量内新增内容 %OPENSSL_HOME%

在这里插入图片描述

1.4. 在 ssl 文件夹下执行命令行操作:
1.4.1.创建私钥: openssl genrsa -des3 -out nj.key 1024
1.4.2.创建csr证书:openssl req -new -key nj.key -out nj.csr
1.4.3.复制文件: copy nj.key nj.key.copy
1.4.4.去除密码: openssl rsa -in nj.key.copy -out nj.key
1.4.4.生成 crt 证书: openssl x509 -req -days 365 -in nj.csr -signkey nj.key -out nj.crt

在这里插入图片描述

五、nginx 配置ssl、 实现http转https

1.1.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       443 ssl;
        server_name  mt.hello.com;
		#ssl          on;
		ssl_certificate     ../ssl/nj.crt;
		ssl_certificate_key ../ssl/nj.key;
		ssl_session_cache shared:SSL:1m;
		ssl_session_timeout 5m;
		ssl_ciphers HIGH:!aNULL:!MD5;
		ssl_prefer_server_ciphers on;
		
		location /chat/ {
            proxy_pass http://jd.hello.com:8089;
        }
	}
	
	server {
		listen 80;
    
		#填写绑定证书的域名
		server_name mt.hello.com;
    
		#强制将http的URL重写成https
		rewrite ^(.*) https://$server_name$1 permanent; 
	}
	
	server {
        listen       8081;
        server_name  mt.hello.com;
		
		location /chat/ {
            proxy_pass http://jd.hello.com:8089;
        }

        location /proxy/nginx/ {
			if ($host = 'mt.hello.com') {
                rewrite ^(.*)$ http://jd.hello.com/$1 permanent;
            }
            proxy_pass http://jd.hello.com:8089/;
        }

       location /proxy/ {
			if ($host = 'mt.hello.com') {
                rewrite ^(.*)$ http://www.baidu.com permanent;
            }
            proxy_pass http://jd.hello.com:8089/;
        }
		
		location /error/ {
			if ($host = 'jd.hello.com') {
                return 404;
            }
            proxy_pass http://jd.hello.com:8089/;
        }
		
		error_page 404 /404.html;
		error_page 500 502 503 504 /50x.html;
		location =/50x.html{
			root html;
		}
		
		location =/404.html{
			root html;
		}

		location /mystatic {
            root html;
			index index.html index.htm;
        }
    }
    
}

1.2.配置ssl证书,443为https的默认端口、实现http协议转https协议

在这里插入图片描述

六、nginx 配置rewrite实现新旧域名平滑更换、配置错误代码转发

在这里插入图片描述

七、静态资源映射

	   location /error/ {
			if ($host = 'jd.hello.com') {
                return 404;
            }
            proxy_pass http://jd.hello.com:8089/;
        }
		
		error_page 404 /404.html;
		error_page 500 502 503 504 /50x.html;
		location =/50x.html{
			root html;
		}
		
		location =/404.html{
			root html;
		}
		
		#https://mt.hello.com/mystatic/index.html
		location /mystatic {
            root html;
			index index.html index.htm;
        }
		
		#https://mt.hello.com/request/request.html
		location /request {
            root html;
			index index.html index.htm;
        }
		
		#https://mt.hello.com/slider/src/images/Pic0.jpg
		location /slider {
            root html;
			index index.html index.htm;
        }

在这里插入图片描述

七、nginx快速安装教程(nginx-1.22.1-版本)

rpm -qa | grep gcc

yum -y install gcc zlib-devel openssl-devel pcre-devel

groupadd nginx
useradd -r -g nginx -s /sbin/nologin nginx

cd nginx-1.22.1/
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module
make && make install

lscpu | grep '^CPU(s)' | awk '{print $2}'

cd /usr/local/nginx && vim conf/nginx.conf
server_tokens off;


ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

nginx -V
nginx -s reload

curl -I 127.0.0.1
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值