欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

nginx域名重写 不加www 转到加www.

程序员文章站 2022-04-09 13:18:10
...
server {
	listen       80;
	server_name www.xxxx.net xxxx.net;
	#配置发布目录为/usr/local/nginx/data/rw-ows
	root  /usr/local/nginx/data/rw-ows;
	#添加以下405代码
	error_page 405 =200 $uri; 
	location / {
		proxy_next_upstream http_502 http_504 error timeout invalid_header;
		proxy_set_header Host  $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://rw-ows;
               #=========================CORS nginx配置=============================
               if ($http_origin ~* (https?://[^/]*\.roamwifi\.net(:[0-9]+)?)$) {
                 set $cors "true";
                }
               if ($request_method = 'OPTIONS') {
                     set $cors "${cors}options";  
                 }
    		if ($request_method = 'GET') {
                   set $cors "${cors}get";  
    		}
    		if ($request_method = 'POST') {
     		   set $cors "${cors}post";
    		}
   		if ($cors = "trueget") {
      		  add_header 'Access-Control-Allow-Origin' "$http_origin";
     		  add_header 'Access-Control-Allow-Credentials' 'true';
    		}
    		if ($cors = "truepost") {
       		 add_header 'Access-Control-Allow-Origin' "$http_origin";
       		 add_header 'Access-Control-Allow-Credentials' 'true';
       		 }
   		 if ($cors = "trueoptions") {
       		 #add_header 'Access-Control-Allow-Origin' "$http_origin";
                  add_header 'Access-Control-Allow-Origin' '*';  
       		 add_header 'Access-Control-Allow-Credentials' 'true';
       		 add_header 'Access-Control-Max-Age' 1728000;
       		 add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
                 add_header 'Content-Length' 0;
                 add_header 'Content-Type' 'text/plain charset=UTF-8';
                 #return 200;
                  return 204;
     		 }
	}
	#动态页面交给http://rw-ows,
	location ~ .*\.(jsp|do)?$ {
		proxy_set_header Host  $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://rw-ows;
	}
	#配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
	location ~ .*\.(html|gif|jpg|jpeg|bmp|png|ico|txt|js|css|swf)$ {
		root /usr/local/nginx/data/xxxx;
		#expires定义用户浏览器缓存的时间为1天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
		expires      1d;
	}
	
        location = /anon_login.jsp {
                return         301 https://$server_name$request_uri;
        }
        location = /anon_register.jsp {
                return         301 https://$server_name$request_uri;
        }
        location = /anon_forgetpwd.jsp {
                return         301 https://$server_name$request_uri;
        }
        if ( $host != 'www.xxxx.net' ) {
          rewrite "^/(.*)$" http://www.xxxx.net/$1 permanent; 
         } 
}

以上就介绍了nginx域名重写 不加www 转到加www.,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。