直接上案例:
需求:
- www.lanmps.com反向代理 github.lanmps.com 这个域名
- 设置 robots.txt 搜索引擎机器人文件
- 配置免费申请域名 SSL 【所有权验证】文件
风.fox
proxy_connect_timeout 300s; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 300s; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_send_timeout 300s;
proxy_buffer_size 64k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
proxy_ignore_client_abort on; #不允许代理端主动关闭连接
proxy_cache_path /www/nginx-cache levels=1:2 keys_zone=nginx-cache:20m max_size=50g inactive=168h;
server
{
listen 80;
server_name www.lanmps.com lanmps.com;
location ^~ /path {
deny all;
}
# 设置 robots.txt 搜索引擎机器人文件
location =/robots.txt {
root /www/wwwroot/www.lanmps.com/;
#fastcgi_intercept_errors on;
#error_page /robots.txt;
index robots.txt;
}
# 配置免费申请域名 SSL 【所有权验证】文件
location /.well-known/pki-validation {
alias /www/wwwroot/www.lanmps.com/.well-known/pki-validation/;
index fileauth.txt;
}
# 反向代理
location / {
proxy_redirect off;
proxy_set_header Host "github.lanmps.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://github.lanmps.com;
proxy_cache nginx-cache;
proxy_cache_valid 200 24h;
proxy_ignore_headers Set-Cookie Cache-Control;
proxy_hide_header Cache-Control;
proxy_hide_header Set-Cookie;
}
access_log /www/wwwLogs/github.lanmps.com.log;
}
可以访问 下列网址看看效果:
http://www.lanmps.com/robots.txt
http://www.lanmps.com/.well-known/pki-validation/fileauth.txt
原始 无文件:
github.lanmps.com/robots.txt
github.lanmps.com/.well-known/pki-validation/fileauth.txt
SSL 配置
首先 在服务器中 nginx.conf 配置文件同级目中新建文件夹ssl
,
把申请到的ssl证书压缩包文件
解压缩后得到 www.lanmps.com.crt
和www.lanmps.com.key
,并上传至服务器 ssl
文件夹内。
proxy_connect_timeout 300s; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 300s; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_send_timeout 300s;
proxy_buffer_size 64k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
proxy_ignore_client_abort on; #不允许代理端主动关闭连接
proxy_cache_path /www/nginx-cache levels=1:2 keys_zone=nginx-cache:20m max_size=50g inactive=168h;
server
{
listen 80;
listen 443;
server_name www.lanmps.com lanmps.com;
# 开启ssl
ssl on;
#缓存有效期
ssl_session_timeout 65m;
#安全链接可选的加密协议
ssl_protocols TLSv1.3 TLSv1.2 TLSv1.1 TLSv1;
#加密算法
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#使用服务器端的首选算法
ssl_prefer_server_ciphers on;
# ssl证书地址
ssl_certificate ssl/www.lanmps.com.crt;
ssl_certificate_key ssl/www.lanmps.com.key;
location ^~ /path {
deny all;
}
# 设置 robots.txt 搜索引擎机器人文件
location =/robots.txt {
root /www/wwwroot/www.lanmps.com/;
#fastcgi_intercept_errors on;
#error_page /robots.txt;
index robots.txt;
}
# 配置免费申请域名 SSL 【所有权验证】文件
location /.well-known/pki-validation {
alias /www/wwwroot/www.lanmps.com/.well-known/pki-validation/;
index fileauth.txt;
}
# 反向代理
location / {
proxy_redirect off;
proxy_set_header Host "github.lanmps.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://github.lanmps.com;
proxy_cache nginx-cache;
proxy_cache_valid 200 24h;
proxy_ignore_headers Set-Cookie Cache-Control;
proxy_hide_header Cache-Control;
proxy_hide_header Set-Cookie;
}
access_log /www/wwwLogs/github.lanmps.com.log;
}
最后重启 nginx 。
即可用 https://www.lanmps.com 访问了
访问java 项目
把如下内容保存到/etc/nginx/conf.d
文件夹 test.foxwho.com.conf
文件中,重启nginx
命令systemctl restart nginx
server {
# 监听端口
listen 80;
listen [::]:80;
#域名
server_name test.foxwho.com;
#重定向到 https 页面
return 301 https://test.foxwho.com$request_uri;
}
server {
# 监听端口 把ssl on;这行去掉,ssl写在443端口后面。这样http和https的链接都可以用
listen [::]:443 ssl;
listen 443 ssl;
#域名
server_name test.foxwho.com;
#静态文件目录
root /www/wwwroot/test.foxwho.com/static;
# ssl证书地址
# pem文件的路径
ssl_certificate /etc/nginx/cert/test.foxwho.com.pem;
# key文件的路径
ssl_certificate_key /etc/nginx/cert/test.foxwho.com.key;
# ssl验证相关配置
#缓存有效期
ssl_session_timeout 65m;
#加密算法
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#安全链接可选的加密协议
ssl_protocols TLSv1.3 TLSv1.2 TLSv1.1 TLSv1;
#使用服务器端的首选算法
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
#错误文件
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|thumb) {
root root /www/wwwroot/test.foxwho.com/static;
expires 10d;
# 文件不存在时
if (!-e $request_filename) {
#跳转
#rewrite ^/data/attachment/forum/(.*)$ http://img.xxx.com/forum/$1 permanent;
#跳转到指定页面
#rewrite ^/(.*)$ /404.html redirect;
#直接报404错误
return 404;
}
}
# 静态文件
location /static/ {
access_log off;
expires 1d;
root /www/wwwroot/test.foxwho.com/static/static;
try_files $uri @back;
}
#其他链接处理
location / {
try_files /_not_exists_ @back;
}
#代理java服务
location @back {
proxy_pass_header Server;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://localhost:8080;
}
#日志
access_log /www/wwwroot/test.foxwho.com.443.log main;
}
注意:把ssl on;这行去掉,ssl写在443端口后面。这样http和https的链接都可以用
如果有必要可以在nignx.conf
配置文件中 http {
内增加如下
proxy_connect_timeout 300s; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 300s; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_send_timeout 300s;
proxy_buffer_size 64k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
proxy_ignore_client_abort on; #不允许代理端主动关闭连接
proxy_cache_path /www/nginx-cache levels=1:2 keys_zone=nginx-cache:20m max_size=50g inactive=168h;
nginx 操作命令
测试配置文件是否正确
nginx -t
如果输出如下表示正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx 启动
systemctl start nginx
nginx 关闭
systemctl stop nginx
nginx 重启
systemctl restart nginx
nginx 状态
systemctl status nginx
nginx 目录
# 配置文件目录
/etc/nginx/
# 配置文件
/etc/nginx/nginx
# 站点目录
/etc/nginx/conf.d