linux下的nginx配置
# proxy_pass路径不加'/'的话,会把匹配命中的路径也代理过去
# 假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问。
第一种:
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html
第二种(相对于第一种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html
第三种:
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html
第四种(相对于第三种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html
场景二:nginx隐藏版本号
curl -I http://192.168.10.19
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
#20行左右;添加;关闭版本号
server_tokens off;
......
}
systemctl restart nginx
curl -I http://192.168.10.19
场景三:生成自签名证书并设置nginx强转https
1、生成自签名证书
mkdir -p /opt/nginx/cert
# 生成带密码的私有密钥文件
openssl genrsa -des3 -out nginx_test.key
# 生成不带密码的私有密钥文件
openssl rsa -in nginx_test.key -out nginx_test.key
# 生成证书文件
openssl req -new -x509 -key nginx_test.key -out nginx_test-ca.crt -days 3650
# 生成证书基本信息nginx_test.csr
openssl req -new -key nginx_test.key -out nginx_test.csr
# 生成签名证书
openssl x509 req -days 3650 -in nginx_test.csr -CA nginx_test-ca.crt -CAkey nginx_test.key -CAcreateserial -out nginx_test.crt
# 生成pfx证书
openssl pkcs12 -export -out nginx_test.pfx -inkey nginx_test.key -in nginx_test.crt
# 生成pem证书(用于导入导出)
cat nginx_test.crt nginx_test.key > nginx_test.pem
2、配置证书以及nginx强转
# 因为监听了两个端口号 导致http和https都能访问 注:443是监听ssl证书的端口 80是默认端口 强制跳转如下
server {
listen 80;
server_name xxxx.com www.xxxx.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name xxxx.com www.xxxx.com;
ssl_certificate /usr/local/server/cert/xxxx.com_bundle.pem;
ssl_certificate_key /usr/local/server/cert/xxxx.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://xxx.xx.xxx.xxx:8888;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
日拱一卒 功不唐捐