(1)配置Nginx端口复用给多个网站,都可以使用80端口去进行访问。
首先需要更改/usr/local/nginx/conf/nginx.conf的配置文件,如下
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include /usr/local/nginx/WebServer/*.conf; #其他代码都是默认的,注意这一行,“ *.conf ”代表
#这个目录下的所有.conf结尾的都是网站的配置文件
}
(2)以/usr/local/nginx/WebServer/vhostH.conf配置文件为例,具体如下:
server {
listen 80; #监听端口
server_name www.liaqi.com; #网站域名
server_name liaqi.com; ##网站域名
rewrite ^(.*) https://$server_name$1 permanent; #进行URL重写,将http访问重写至https
}
server {
listen 443 ssl;
server_name www.liaqi.com;
index index.html;
root /usr/local/nginx/WebServer/vhostH; #网站目录文件
ssl_certificate /usr/local/nginx/sslkey/vhostH/full_chain.pem; #证书存放路径
ssl_certificate_key /usr/local/nginx/sslkey/vhostH/private.key; #证书存放路径
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
server_tokens off;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
access_log /usr/local/nginx/logs/httpsaccess.log;
}
按照(2)中的配置文件进行更改即可,即可部署多个网站的80端口复用。
比如你有两个网站,一个是www.liaqi.com另外一个是www.liaqi.cn,www.liaqi.com备案了,但是www.liaqi.cn没有进行备案,那么可以通过Nginx重定向的方法,把网站的访问给重定向到www.liaqi.com上面去。
备案域名的Nginx配置如下:
server {
listen 80;
server_name www.liaqi.com;
server_name liaqi.com;
rewrite ^(.*) https://$server_name$1 permanent; #进行URL重写,将http访问重写至https
}
server {
listen 443 ssl;
server_name www.liaqi.com;
server_name liaqi.com;
index index.html;
root /usr/local/nginx/WebServer/vhostA; #网站目录文件
ssl_certificate /usr/local/nginx/sslkey/vhostA/full_chain.pem;
ssl_certificate_key /usr/local/nginx/sslkey/vhostA/private.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
server_tokens off;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
access_log /usr/local/nginx/logs/httpsaccess.log;
}
未备案域名的Nginx配置如下:
server {
listen 80;
listen 443 ssl;
server_name www.liaqi.cn;
server_name liaqi.cn;
ssl_certificate /usr/local/nginx/sslkey/vhostB/full_chain.pem;
ssl_certificate_key /usr/local/nginx/sslkey/vhostB/private.key;
rewrite ^(.*) https://www.liaqi.com permanent; #重写域名至具体网站
rewrite ^(.*) https://liaqi.com permanent; #重写域名至具体网站
}
一个网站,我想使用80端口进行访问,但是没有备案,那么该使用Nginx如何配置?(注:本段仅代表个人观点,请按照相关网站要求进行备案)
具体配置方法如下:
server {
listen 80; #监听80和443端口访问
listen 443; #监听80和443端口访问
server_name www.liaqi.com;
server_name liaqi.com;
rewrite ^(.*) https://www.liaqi.com:8887 permanent; #重写至服务器的8887端口
rewrite ^(.*) https://liaqi.com:8887 permanent; #重写至服务器的8887端口
}
server {
listen 8887 ssl; #对需要重写的8887端口进行开启https访问
server_name 192.168.0.112.92:8887; #开启使用的8887端口
index index.html;
#server_tokens off;
default_type application/octet-stream;
ssl_certificate /usr/local/nginx/sslkey/vhostA/full_chain.pem;
ssl_certificate_key /usr/local/nginx/sslkey/vhostA/private.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
server_tokens off;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
access_log /usr/local/nginx/logs/httpsaccess.log;
location / {
proxy_pass https://192.168.0.21:8887; #可以是服务自己的IP地址和端口,也可以是其他服务的IP地址和端口
index index.htm index.html;
}
}
#配置的大概意思就是:源站点是https://192.168.0.21:8887,但是是带有端口的,但是无法通过80端口访问,
#所以我用这个https://192.168.0.112.92:8887来进行代理源站点。并且www.liaqi.com的域名已经解析至这
#台服务器上了,所以在浏览器直接输入域名即可正常的访问到目标站点。另外,此方法不保证长久有效,有些服务
#提供商发现有人使用此种方法会进行封停相关端口,慎用慎用。
我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3m3shdxom4o4k
我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3m3shdxom4o4k