由于公司需求,近期修复了域名访问的问题,具体nginx配置如下
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.xxx.cn xxx.cn;
index index.html ;
location ~* ^/(index|gsdt|clt|runman|gywm|fuwushang)\.html$ {
rewrite ^.*$ /xxx/html/$request_uri redirect;
}
if ($host = 'xxx.cn') {
rewrite ^/(.*)$ http://www.xxx.cn/$1 permanent;
}
location /{
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $Host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80 ;
server_name www.xxx.com xxx.com;
index index.html;
location ~* ^/(index|gsdt|clt|runman|gywm|fuwushang)\.html$ {
rewrite ^.*$ /xxx/html$request_uri redirect;
}
if ($host = 'xxx.com') {
rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;
}
location /{
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $Host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
主要是做了xxx.cn和xxx.com域名的主页重定向和反向代理
第一段location控制了xxx.cn/index.html等具体几个文件的重定向,以解决百度快照和360快照内的访问问题
第二段if控制了无主机头访问时301永久重定向到www主机头的访问
第三段location控制了反向代理
阿里云ECS实例升级、续费,享低至 6.3折 限时折扣
https://www.aliyun.com/daily-act/ecs/care?userCode=kkqw2ds2
码字不易,各位看官,领个优惠券的同时别忘记点赞哦