简体   繁体   English

Nginx + Nodejs + WordPress 路由

[英]Nginx + Nodejs + WordPress routing

So I installed Nginx, Nodejs, PHP, MySQL and WordPress on ec2 server.所以我在 ec2 服务器上安装了 Nginx、Nodejs、PHP、MySQL 和 WordPress。 The node app works fine using the root dir and [example.com] as example.使用根目录和 [example.com] 作为示例,节点应用程序可以正常工作。 When trying to access the blog in [example.com/blog], I get an 404 error in the logs and a screen message Cannot GET /blog.尝试访问 [example.com/blog] 中的博客时,我在日志中收到 404 错误和屏幕消息无法获取 /blog。

What am I doing wrong in this nginx config file?我在这个 nginx 配置文件中做错了什么?

server {
    listen 80;
    server_name example.com;
    access_log /var/log/nginx/mysite.log;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx$
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
      proxy_pass http://example.com:3000;
      proxy_redirect off;
    }
    location /blog/ {
            # try_files $uri $uri/ =404;
            root /home/ec2-user/mysite/blog;
            try_files $uri $uri/ /index.php?q=$uri&$args;
            # proxy_pass http://example.com/blog;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/html;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            # proxy_pass http://example.com/blog;

    }
 }

EDIT: After gathering tips from various tutorials, my config file looks like this:编辑:从各种教程中收集提示后,我的配置文件如下所示:

 server {
listen 80;
server_name example.com;
access_log /var/log/nginx/nginx-access.log;
error_log /var/log/nginx/nginx-error.log;
root /home/ec2-user/example;

# pass the request to the node.js server with the correct headers and much more can be added, see nginx$
location / {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
  proxy_pass http://example.com:3000;
  proxy_redirect off;
}

location /blog/ {
    index index.php;
    try_files $uri $uri/ /index.php?q=$uri&$args;

     location ~ \.php$ {
        try_files $uri =404;
        include fastcgi.conf;
        fastcgi_split_path_info ^()(.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index index.php;

        }

        if (!-e $request_filename)
        {
            rewrite ^(.+)$ /index.php?q=$1 last;
        }
}
 location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|ttf|woff|eot|svg)$ {
    alias   /home/ec2-user/example/public;
}

} }

The 404 isn't fixed though...ANY help will be appreciated. 404 不是固定的...任何帮助将不胜感激。

Finally the routing for both node and wordpress works!最后,node 和 wordpress 的路由都起作用了! (with the help of this answer ) (在这个答案的帮助下)

 server {
listen 80;
server_name example.com;
access_log /var/log/nginx/nginx-access.log;
error_log /var/log/nginx/nginx-error.log;
root /home/ec2-user/example;

# pass the request to the node.js server with the correct headers and much more can be added, see nginx$
location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
      proxy_pass http://example.me:3000;
      proxy_redirect off;
     location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|ttf|woff|eot|svg)$ {
        alias   /home/ec2-user/example/public;
    }
}


location @blog {
  rewrite ^/blog(.*) /blog/index.php?q=$1;
}
location /blog {
    index index.php;
    try_files $uri $uri/ @blog;
    alias /home/ec2-user/example/blog;

     location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass 127.0.0.1:9000;
        }

        #if (!-e $request_filename)
        #{
        #   rewrite ^(.+)$ /index.php?q=$1 last;
        #}
} }

I was working with centos linux distro had a few changes to make to我正在使用 centos linux distro 进行一些更改

server {
listen 80;
server_name example.com;

access_log /var/log/nginx/nginx-access.log;
error_log /var/log/nginx/nginx-error.log;

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_pass http://127.0.0.1:5000;
    proxy_redirect off;
    proxy_set_header   X-Forwarded-For $remote_addr;


    error_page 404 /404;
    error_page 500 502 503 504 /404;

    location ~ (jpg|jpeg|gif|png|ico|css|js|ttf|woff|eot|svg|js|css) {
      root   /var/www/html/example/views;
    }
}

location @blog {
    rewrite ^/blog(.*) /blog/index.php?$args;
}

location /blog {
    index index.php;
    try_files $uri $uri/ @blog;
    alias /var/www/html/blog;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_index   /blog/index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include fastcgi_params;
    }


    #if (!-e $request_filename)
    #{
    #   rewrite ^(.+)$ /index.php?q=$1 last;
    #}
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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