简体   繁体   中英

Simple rewrite rule for a nginx + apache2 with mod_wsgi

I'm stuck with this, my skills in the web servers area are poor...

I have an Nginx acting as a proxy for an Apache2 running with mod_wsgi and mod_rewrite. What I want to do is rewrite every URL from www.example.com to example.com, ie stripping the www part from each URL request before serving. This is the layout of the different conf files:

=== /etc/nginx/sites-enabled/example.com ===:

http://dpaste.com/82638/

=== /etc/apache2/sites-enabled/example.com ===:

http://dpaste.com/hold/82645/

=== /home/nabuco/public_html/example.com/example/apache/example.wsgi ===:

http://dpaste.com/82643/

In my old set up I had an Apache2 running mod_python, and the only thing I had to do was putting an .htaccess file like this:

Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

That worked perfectly.

But I tried putting the same .htaccess file into /home/nabuco/public_html/nomadblue.com/nomadblue/apache/.htaccess. If I cast a request without leading www, such as http://example.com/ or http://example.com/whatever , everything goes well. However, if I try the www version of http://www.example.com/ I am redirected to:

http://example.com/example.wsgi/

Do I have to run rewriting rules from nginx instead? I tried that too, adding this to the nginx conf file:

rewrite ^/(.*) http://example.com/$1 permanent;

but now I am getting what firefox calls a "circular loop"...

So who can I get this (I guess trivial) thing up?

Thanks in advance,

Hector

The easiest is to rewrite with nginx. Put that rewrite rule in a dedicated "server" bound to www.example.com

server {
  listen 80;
  server_name www.example.com;
  rewrute ^/(.*) http://example.com/$1 permanent;
}

好的,我找到了避免循环的解决方案...通过在我的nginx配置文件中创建两个server节,一个用于www.example.com-具有rzab建议的重写规则-另一个例如。 com,其中包含所有其他指令。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM