前言: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;
}
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);
String url = "http://10.9.103.35:8083/html/chat/mystatic/";
三、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