Nginx网站重定向

人生难得几回搏,此时不搏待何时。这篇文章主要讲述Nginx网站重定向相关的知识,希望能为你提供帮助。
1、nginx配置多网站端口复用
(1)配置Nginx端口复用给多个网站,都可以使用80端口去进行访问。
首先需要更改/usr/local/nginx/conf/nginx.conf的配置文件,如下

#usernobody;

worker_processes1;
events
worker_connections1024;


http
includemime.types;
default_typeapplication/octet-stream;
sendfileon;
keepalive_timeout65;

server
listen80;
server_namelocalhost;
#charset koi8-r;
#access_loglogs/host.access.logmain;
location /
roothtml;
indexindex.html index.htm;


error_page500 502 503 504/50x.html;
location = /50x.html
roothtml;


include /usr/local/nginx/WebServer/*.conf; #其他代码都是默认的,注意这一行,“ *.conf ”代表
#这个目录下的所有.conf结尾的都是网站的配置文件




(2)以/usr/local/nginx/WebServer/vhostH.conf配置文件为例,具体如下:
server
listen80; #监听端口
server_namewww.liaqi.com; #网站域名
server_nameliaqi.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_cacheshared:SSL:1m;
ssl_session_timeout5m;
server_tokens off;
fastcgi_paramHTTPSon;
fastcgi_paramHTTP_SCHEMEhttps;
access_log /usr/local/nginx/logs/httpsaccess.log;



按照(2)中的配置文件进行更改即可,即可部署多个网站的80端口复用。


2、网站80和443网站重定向
比如你有两个网站,一个是www.liaqi.com另外一个是www.liaqi.cn,www.liaqi.com备案了,但是www.liaqi.cn没有进行备案,那么可以通过Nginx重定向的方法,把网站的访问给重定向到www.liaqi.com上面去。
备案域名的Nginx配置如下:
server
listen80;
server_namewww.liaqi.com;
server_nameliaqi.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_cacheshared:SSL:1m;
ssl_session_timeout5m;
server_tokens off;
fastcgi_paramHTTPSon;
fastcgi_paramHTTP_SCHEME https;
access_log /usr/local/nginx/logs/httpsaccess.log;



未备案域名的Nginx配置如下:
server
listen80;
listen443 ssl;
server_namewww.liaqi.cn;
server_nameliaqi.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; #重写域名至具体网站





3、配置80端口重定向到其他端口
一个网站,我想使用80端口进行访问,但是没有备案,那么该使用Nginx如何配置?(注:本段仅代表个人观点,请按照相关网站要求进行备案)
具体配置方法如下:
server

listen80; #监听80和443端口访问
listen443; #监听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
listen8887 ssl; #对需要重写的8887端口进行开启https访问
server_name 192.168.0.112.92:8887; #开启使用的8887端口
index index.html;
#server_tokens off;
default_typeapplication/octet-stream;
ssl_certificate /usr/local/nginx/sslkey/vhostA/full_chain.pem;
ssl_certificate_key /usr/local/nginx/sslkey/vhostA/private.key;
ssl_session_cacheshared:SSL:1m;
ssl_session_timeout5m;
server_tokens off;
fastcgi_paramHTTPSon;
fastcgi_paramHTTP_SCHEMEhttps;
access_log /usr/local/nginx/logs/httpsaccess.log;
location /
proxy_pass https://192.168.0.21:8887; #可以是服务自己的IP地址和端口,也可以是其他服务的IP地址和端口
indexindex.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

【Nginx网站重定向】


    推荐阅读