大网数据广告活动
服务器运维问答

Redirect 301 issue Apache behind Nginx

· 52CE 我爱测

My non-www example.com has been redirected to www.example.com/index.php . Note: I'm redirecting using .htaccess and not with nginx virtual host file. RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule (.*) http://www.example.…

问题原文

原帖链接 · Renan Souza · CC BY-SA 3.0

My non-www example.com has been redirected to www.example.com/index.php.

Note: I'm redirecting using .htaccess and not with nginx virtual host file.

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

If I comment the lines in .htaccess and try to redirect through nginx virtual host file curl respond correctly as set but I received a 404 (model by my site) when access www.example.com.

By the way, is it better to redirect this through .htaccess or nginx? As I'm using nginx as proxy.

server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;



location / {
    try_files $uri $uri/ @proxy;
}

location @proxy {
    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;
    proxy_set_header X-Forwarded-Proto $scheme;
}

location ~ \.php$ {
    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;
    proxy_set_header X-Forwarded-Proto $scheme;
}
}

Virtual Host code Nginx

server {
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}

curl -I example.com

 HTTP/1.1 301 Moved Permanently
 Date: Mon, 04 Dec 2017 20:39:54 GMT
 Content-Type: text/html
 Connection: keep-alive
 Set-Cookie: __cfduid=d7048cb8bebb2c847d904c46cd78d8f0c1512419; 
 expires=Tue, 04-Dec-18 20:39:54 GMT; path=/; domain=.example.com; HttpOnly
 Location: http://www.example.com/
 Server: cloudflare-nginx
 CF-RAY: 3c8193e88245-EWR

All site is working fine but just at the index I receive a 403 Forbidden. Note: it's a internal redirect so I don't know which one nginx or apache is better for performance. In apache non-www is redirect to www.example.com/index.php (I don't want this .index.php and in nginx is redirected normally but I receive a 403 at homepage with the rest of the site working fine.

已采纳解答

原帖链接 · Renan Souza · CC BY-SA 3.0

I was putting the redirect inside the same server {} block and I needed to put in a separate server {} block just for the example.com host.

server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}


server {
    listen 80;
    server_name www.example.com;

    location / {
        try_files $uri $uri/ @proxy;
    }

    location @proxy {
        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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ~ \.php$ {
        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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-12-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。