nginx反向代理回退

时间:2019-04-16 10:41:06

标签: wordpress nginx reverse-proxy

我正在通过反向代理为wordpress提供服务(wordpress在docker上)。 我还想使用相同的server_name来提供静态登录页面。 我尝试过:

server {
    error_log  /var/log/nginx/error.log debug;
    listen            80;
    server_name ${NGINX_HOST};
    sendfile          on;
    tcp_nodelay       on;
    root              /static-pages;
    index             index.html index.php;

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

    location @wordpress {
        proxy_read_timeout    90;
        proxy_connect_timeout 90;
        proxy_redirect        off;
        proxy_pass http://wordpress;

        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;
        proxy_set_header      Host $http_host;
    }
}

静态页面可以工作,但是/根(wordpress反向代理)返回403禁止的错误(由于它不存在,可能无法索引/static-pages/index.html)。 配置是否符合我的需求?谢谢。

请记住,wordpress也有使用该URI的页面:/about/jobs,并且这些目录不在/static-pages下,因此应该到达@wordpress位置。

我的docker-compose.yml

version: '3.7'
services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    volumes:
       - db_data:/var/lib/mysql
  wordpress:
    image: wordpress
    restart: always
    depends_on:
       - db
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DEBUG: 1
      WORDPRESS_CONFIG_EXTRA: |
        define('WP_HOME', 'http://localhost:8080');
        define('WP_SITEURL', 'http://localhost:8080');
    volumes:
      - ../wordpress-nisite:/var/www/html
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini

  nginx:
    image: nginx
    ports:
      - "8080:80"
    depends_on:
      - wordpress
    restart: always
    volumes:
     - ./nginx-configs/main.template:/etc/nginx/conf.d/main.template
     - ../:/static-pages/
    environment:
      - "NGINX_HOST=localhost:8080"
    ports:
     - "8080:80"
    command: /bin/bash -c "envsubst '$${NGINX_HOST}' < /etc/nginx/conf.d/main.template > /etc/nginx/conf.d/default.conf && exec nginx-debug -g 'daemon off;'"

volumes:
  db_data:
  

参考:https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#proxy-everything

0 个答案:

没有答案