【发布时间】:2019-10-29 17:40:28
【问题描述】:
我是 nginx 新手。我尝试使用搜索 www 和 stackoverflow 来学习。大多数情况下,我会得到帮助来了解如何构建我的 nginx 配置。
我的域是 www.mysite.com。一切; 着陆页,错误和服务器错误必须重定向到默认index.html。我还定义了我的访问和错误日志。所有这些都在 /etc/nginx/conf.d/default.conf 中完成(如下代码)。
我需要重定向 (proxy_pass) /admin、/user 以及与它们相关的任何内容。例如 /admin 也有不同的文件夹,如 /admin/login/。我需要重定向 /admin 之后的所有内容。 /user 也是如此。
1) 查看我的代码,我是否正确重定向了 location /admin 和 location /user?
2) 我也用 try_files $uri $uri/ =404;在重定向中。它还将 404 重定向到默认 index.html。我做得对吗?
3) 我也拒绝访问某些文件和文件夹。我做得对吗?
我的主要问题是如何纠正我的 nginx 配置,如果它是错误的?因此,为了理解正确的 nginx 配置,我将我的问题分为上述 3 个不同的问题。我希望我没有阻止 stackoverflow 如何提出问题指南。
谢谢。
更新:
server {
charset UTF-8;
listen 80;
listen [::]:80;
server_name www.mysite.com;
access_log /var/log/nginx/host.access.log main;
error_log /var/log/nginx/host.error.log main;
# define a root location variable and assign a value
set $rootLocation /usr/share/nginx/html;
# define www.mysite.com landing page to the static index.html page
location / {
root rootLocation;
index index.html index.htm;
}
# define error page to the static index.html page
error_page 404 /index.html;
location = /index.html {
root rootLocation;
internal;
}
# redirect server error pages to the static index.html page
error_page 500 502 503 504 /index.html;
location = /index.html {
root rootLocation;
internal;
}
# redirect www.mysite.com/admin to localhost
# /admin or /admin/ or /admin/**** must be redirect to localhost
location /admin {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:3000";
}
# redirect www.mysite.com/user to localhost
# /user or /user/ or /user/**** must be redirect to localhost
location /user {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:3001";
}
}
【问题讨论】:
标签: nginx redirect configuration proxypass