Nginx版本:1.16.0
Tomcat版本:8.5.41
系统环境:CentOS Linux release 7.6.1810
Nginx在配置多Tomcat多域名时,需要代理多个Tomcat端口,我将所有域名的配置文件都放在Nginx的运行目录下(/usr/local/nginx/conf/host),host为新建的文件夹,每一个域名有一份配置文件。
将Nginx的配置文件中的server部分全部删除并且在http的大括号里面加入下面一段:
include /usr/local/nginx/conf/host/*.conf;
表示引入外部配置文件。
-
配置https
准备材料:
域名
域名证书
关于服务器和域名以及证书这块可以查看我的另外一篇文章:https://blog.csdn.net/u012667477/article/details/82793183
配置内容:
server {
listen 443 ssl;
server_name mp.idwarf.cn;
ssl_certificate ssl/1_mp.idwarf.cn_bundle.crt;
ssl_certificate_key ssl/2_mp.idwarf.cn.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
proxy_pass http://mp;
}
}
-
http自动跳转https
配置内容:
listen 80;
server_name mp.idwarf.cn;
return 301 https://mp.idwarf.cn$request_uri;
-
配置全站favicon.ico
准备材料:
ico格式的图标,并将其命名为favicon.ico
配置内容:
location ~ ^/favicon\.ico$ {
root /usr/local/nginx/conf/favicon;
}
其中/usr/local/nginx/conf/favicon为这个图标所在的文件夹目录
-
完整配置文件
upstream mp{
server 127.0.0.1:8081;
}
server {
listen 80;
server_name mp.idwarf.cn;
return 301 https://mp.idwarf.cn$request_uri;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://mp;
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;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#
#location / {
# root /usr/local/nginx/html;
# index index.html index.htm;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/local/nginx/html;
#}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
server {
listen 443 ssl;
server_name mp.idwarf.cn;
ssl_certificate ssl/1_mp.idwarf.cn_bundle.crt;
ssl_certificate_key ssl/2_mp.idwarf.cn.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
proxy_pass http://mp;
}
location ~ ^/favicon\.ico$ {
root /usr/local/nginx/conf/favicon;
}
}
-
Nginx 常用命令
- Nginx下载地址:http://nginx.org/en/download.html
- Nginx启动:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
- 查看Nginx端口:ps -ef | grep nginx
- 停止Nginx:kill -9 上一个命令查询到的端口号
- 重启Nginx:/usr/local/nginx/sbin/nginx -s reload
- 检查Nginx配置文件:/usr/local/nginx/sbin/nginx -t