如何配置nginx为proxy_pass?
How to configure nginx as proxy_pass?
我已经使用 Certbot Nginx 将我的 nginx 安装到 Ubuntu 18.04。我还使用 Certbot 来获取 LetsEncrypt 证书。
我使用基本配置,例如使用 tcp 80 和 433,这样我就可以获得 http 和 https。大部分配置由 Certbot 完成。
我的域名是 http://example.com and provides static page. Bu I also have a folder and I can call it with http://example.com/myfolder.
安装 nginx 后,我尝试使用 proxy_pass 和 https 访问我的本地 rest_api 服务。
如果我输入 http://127.0.0.1:1024/myfolder 然后我可以看到我的 rest-api 工作。
如果我键入 https://example.com/myfolder,则 nginx 不会激活 proxy_pass
。 mystic.com 已注册并工作8个多月。所有的 DNS 配置都在工作。 nginx letsencrypt 也在工作。 1 周前我的证书自动更新(每 90 天)。
我的 Nginx 配置如下。我的 nginx
版本是 1.15.10
.
- nginx如何配置为proxy_pass?
- 我的proxy_pass正确吗?
- 如何更改此行
try_files $uri $uri/ =404;
以显示 var/www/html/index.html
?
server {
charset UTF-8;
listen 80 ;
listen [::]:80 ;
server_name example.com; # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
return 404; # managed by Certbot
}
server {
charset UTF-8;
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
server_name example.com; # managed by Certbot
ssl_certificate /mypath/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /mypath/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
proxy_pass http://127.0.0.1:1024/;
}
}
尝试像这样更改您的基本 Nginx 配置,然后从 certbot 安装 LetsEncrypt 证书:
server {
listen 80;
server_name example.com;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:1024";
}
location /admin {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://192.168.2.1:8080";
}
location /user {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://192.168.2.1:1024";
}
}
它对我来说非常完美certbot.Don在测试配置之前不要忘记重新加载 nginx 服务。
我已经使用 Certbot Nginx 将我的 nginx 安装到 Ubuntu 18.04。我还使用 Certbot 来获取 LetsEncrypt 证书。
我使用基本配置,例如使用 tcp 80 和 433,这样我就可以获得 http 和 https。大部分配置由 Certbot 完成。
我的域名是 http://example.com and provides static page. Bu I also have a folder and I can call it with http://example.com/myfolder.
安装 nginx 后,我尝试使用 proxy_pass 和 https 访问我的本地 rest_api 服务。
如果我输入 http://127.0.0.1:1024/myfolder 然后我可以看到我的 rest-api 工作。
如果我键入 https://example.com/myfolder,则 nginx 不会激活 proxy_pass
。 mystic.com 已注册并工作8个多月。所有的 DNS 配置都在工作。 nginx letsencrypt 也在工作。 1 周前我的证书自动更新(每 90 天)。
我的 Nginx 配置如下。我的 nginx
版本是 1.15.10
.
- nginx如何配置为proxy_pass?
- 我的proxy_pass正确吗?
- 如何更改此行
try_files $uri $uri/ =404;
以显示var/www/html/index.html
?
server {
charset UTF-8;
listen 80 ;
listen [::]:80 ;
server_name example.com; # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
return 404; # managed by Certbot
}
server {
charset UTF-8;
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
server_name example.com; # managed by Certbot
ssl_certificate /mypath/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /mypath/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
proxy_pass http://127.0.0.1:1024/;
}
}
尝试像这样更改您的基本 Nginx 配置,然后从 certbot 安装 LetsEncrypt 证书:
server {
listen 80;
server_name example.com;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:1024";
}
location /admin {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://192.168.2.1:8080";
}
location /user {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://192.168.2.1:1024";
}
}
它对我来说非常完美certbot.Don在测试配置之前不要忘记重新加载 nginx 服务。