简体   繁体   English

Nginx - wordpress 在页面旁边配置 api

[英]Nginx - wordpress config alongside page with api

I have some site on Nginx with page and API for it.我在 Nginx 上有一些带有页面和 API 的站点。 Location of html page is html 页的位置是

var/www/mysite/mysite_rest var/www/mysite/mysite_rest

and location of api is api 的位置是

var/www/mysite/mysite_rest/api var/www/mysite/mysite_rest/api

The next configuration works fine for me:下一个配置对我来说很好:

server {
listen 80;
server_name example.com;
#    return 301 https://$host$request_uri;
return 301 https://example.com;
rewrite ^ https://example.com$request_uri? permanent;

client_max_body_size 350M;
}

server {
listen 443 ssl http2;
server_name example.com;

charset UTF-8;

# certs sent to the client in SERVER HELLO are concatenated in 
ssl_certificate
ssl_certificate /etc/ssl/example.crt;
ssl_certificate_key /etc/ssl/example.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 30m;
keepalive_timeout 70;
ssl_session_tickets off;

ssl_dhparam /etc/ssl/dhparam.pem;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers off;

# ssl_stapling on;
ssl_trusted_certificate /etc/ssl/example.crt;
# resolver 8.8.8.8;

# HSTS (ngx_http_headers_module is required) (63072000 seconds)
add_header Strict-Transport-Security "max-age=63072000" always;
# add_header Strict-Transport-Security max-age=31536000 always;
add_header X-Frame-Options DENY;
# add_header Public-Key-Pins 'pin-sha256="base64+info1="; max-age=31536000' 
always;

add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";

##
rewrite ^/(.*)/$ /$1 permanent;
rewrite ^/api/(.*)$ /api/index.php?_url=/$1 last;
rewrite ^/api$ /api/index.php?_url=/ last;

##

location / {
root /var/www/mysite/mysite_rest;
charset utf-8;
index index.php index.html;
}

location /api {
        internal;
        root /var/www/mysite/mysite_rest/api;
        index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
 #            fastcgi_pass unix:/var/run/php7.1-fpm.sock;
        fastcgi_pass 127.0.0.1:9000;
         #    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
       fastcgi_param SCRIPT_FILENAME /var/www/mysite/mysite_rest/api/index.php;
   fastcgi_param SCRIPT_NAME index.php;
        fastcgi_param HTTPS on;
#        fastcgi_param HTTP_HTTPS on;
    fastcgi_param REQUEST_SCHEME https;
    fastcgi_param SERVER_PORT 443;

   }         
}

And now I need to install wordpress alongside my site, I downloaded it and placed in现在我需要在我的网站旁边安装 wordpress,我下载了它并放置在

var/www/mysite/wp var/www/mysite/wp

folder, then added to Nginx conf the next line:文件夹,然后添加到 Nginx conf 下一行:

    location /wp {
        internal;
        root /var/www/mysite/wp;
        index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
#            fastcgi_pass unix:/var/run/php7.1-fpm.sock;
        fastcgi_pass 127.0.0.1:9000;
        #    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
       fastcgi_param SCRIPT_FILENAME /var/www/mysite/wp/index.php;
   fastcgi_param SCRIPT_NAME index.php;
#            fastcgi_param HTTPS on;
    fastcgi_param HTTP_HTTPS on;
    fastcgi_param REQUEST_SCHEME https;
    fastcgi_param SERVER_PORT 443;
} 

The result of requesting url: example.com/wp is 404. What I'm doing wrong?请求 url:example.com/wp 的结果是 404。我做错了什么?

UPD1 When deleting "internal" the request rewrites on https://example.comindex.php/wp-admin/install.php UPD1 删除“内部”时,请求重写https://example.comindex.php/wp-admin/install.php

UPD2 Add the next lines for wordpressp location instead of previous: UPD2 为 wordpressp 位置添加下一行而不是上一行:

location ^~ /wp {
root /var/www/mysite/mysite_rest;
index index.php index.html index.htm;
try_files $uri $uri/ /wp/index.php;

location ~ \.php {
fastcgi_split_path_info ^(.*\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass 127.0.0.1:9000;
}

This allowed me to run install, but than when I'm requesting example.com/wp it causes ERR_TOO_MANY_REDIRECTS.这允许我运行安装,但是当我请求 example.com/wp 时,它会导致 ERR_TOO_MANY_REDIRECTS。 I can access /wp-login.php by manually request.我可以通过手动请求访问 /wp-login.php。

You have used the internal directive which prevents external access to the location .您使用了internal指令来阻止外部访问location The root directive is wrong and you have no way to load WordPress static files. root指令错误,您无法加载 WordPress static 文件。

You could try something like this:你可以尝试这样的事情:

location ^~ /wp {
    root /var/www/mysite;
    index index.php;

    try_files $uri $uri/ /wp/index.php;

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

Don't forget to configure WordPress with the correct URL.不要忘记用正确的 URL 配置 WordPress。 See this document for details.有关详细信息,请参阅此文档

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

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