我是 nginx 的新手,我想为翼手龙面板进行这个 nginx 网络服务器配置,在端口 8080 而不是 80 上运行
这是我当前的代码
server_tokens off;
server {
listen 80;
server_name mywebsite.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name mywebsite.com;
root /var/www/pterodactyl/public;
index index.php;
access_log /var/log/nginx/pterodactyl.app-access.log;
error_log /var/log/nginx/pterodactyl.app-error.log error;
# allow larger file uploads and longer script runtimes
client_max_body_size 100m;
client_body_timeout 120s;
sendfile off;
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;
ssl_session_cache shared:SSL:10m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
ssl_prefer_server_ciphers on;
# See https://hstspreload.org/ before uncommenting the line below.
# add_header Strict-Transport-Security "max-age=15768000; preload;";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
add_header Content-Security-Policy "frame-ancestors 'self'";
add_header X-Frame-Options DENY;
add_header Referrer-Policy same-origin;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "upload_max_filesize = 100M \n post_max_size=100M";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTP_PROXY "";
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
include /etc/nginx/fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
我尝试过做
server {
listen 8080;
server_name mywebsite.com;
return 301 https://$server_name$request_uri;
}
但是当我使用端口 8080 时,它会给出 ERR_SSL_PROTOCOL_ERROR,即使我将端口 80 更改为端口 8080,端口 80 仍然可以工作 当我使用此 HTTP 配置(不带 SSL)而不是 HTTPS(带 SSL)时,它工作正常
server {
listen 8080;
server_name mywebsite.com;
root /var/www/pterodactyl/public;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/pterodactyl.app-error.log error;
# allow larger file uploads and longer script runtimes
client_max_body_size 100m;
client_body_timeout 120s;
sendfile off;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "upload_max_filesize = 100M \n post_max_size=100M";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTP_PROXY "";
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
tldr:将
listen 443 ssl http2;
更改为 listen 8080 ssl http2;
,然后连接类似 https://localhost:8080
的内容
一些快速的背景知识可以提供帮助。
默认配置中的 Nginx 正在监听两个端口:80 和 443,并且每个端口都使用不同的协议。在端口 80 上,它使用纯 HTTP。在端口 443 上,它使用 HTTPS,这是使用 SSL/TLS 协议加密封装的纯 HTTP(这是一种过于简单化的做法,我意识到它在技术上并不完全准确,但对于本次讨论来说已经足够了)。如果您尝试与 HTTPS 端口进行普通 HTTP 通信,服务器将无法理解,就像用希腊语向韩国调酒师点一杯饮料一样,这会引起混乱。
因此,当您连接到网站时,您需要指定协议、服务器和端口(以及一堆其他内容,这些内容不是本次讨论的重点)。所以连接到
http://servername:8080
就是
http
servername
8080
但是人们讨厌输入数字[需要引用],所以我们有一个约定。如果您使用
http
连接到服务器并且未指定端口号,则您的浏览器(或任何 HTTP 客户端)将默认使用端口 80。如果您使用 https
连接到服务器,则您的 http 客户端将使用 connect到端口 443。所以本质上 https://google.com
被转换为 https://google.com:443
。
但是如果我们使用非标准端口(即不是 80 或 443),那么我们必须确保协议与服务器所提供的服务相匹配。按照宽松的约定,我们倾向于使用 8080 作为替代 HTTP 端口,使用 8443 作为替代 HTTPS 端口——没有什么强制你这样做,它只是有助于减少人们在查看端口号时的一些困惑。
所以你的 nginx 配置正在监听两个端口。
listen 80;
告诉它第一个块正在配置一个可在端口 80 上访问的 Web 服务器,并且使用纯 HTTP(我们知道它是纯 HTTP,因为这是 nginx 的默认设置,并且 listen
行上没有其他参数。
listen 443 ssl http2;
告诉 nginx 该服务器块正在控制可在端口 443 上访问并使用 HTTPS 的 Web 服务器(由于 ssl
参数。我们现在将礼貌地忽略 http2
,因为它是非在此推论)。
因此,当您有一个带有
listen 8080
的块时,您的服务器将可以通过 http://mywebsite.com:8080
访问。但是,您的配置中的该块实际上并不提供任何内容。它有一行 return 301 https://$server_name$request_uri;
,表示每当它收到对此端口的请求时,它都会always回复 HTTP 301 消息,这是一个重定向,告诉客户端他们正在寻找的内容实际上位于 https
,因此客户端(您的浏览器)收到此消息,然后重新连接到相同的地址,但使用 https
,因此当您连接到 http://mywebsite.com:8080/login
时,您的浏览器将被重定向到 https://mywebsite.com/login
。
因此,如果您想通过端口 8080 访问您的内容,那么您需要:
http://mywebsite.com:8080
访问它(我不建议这样做),或者https://mywebsite.com:8080
后一种配置看起来像这样(我还没有测试过)。这只会更改第一个服务器块中的重定向,以便连接到 http://mywebsite.com
的任何人都被重定向到
https://mywebsite.com:8080
以及 HTTPS 块中的侦听端口。server_tokens off;
server {
listen 80;
server_name mywebsite.com;
return 301 https://$server_name:8080$request_uri;
}
server {
listen 8080 ssl http2;
server_name mywebsite.com;
root /var/www/pterodactyl/public;
index index.php;
access_log /var/log/nginx/pterodactyl.app-access.log;
error_log /var/log/nginx/pterodactyl.app-error.log error;
# allow larger file uploads and longer script runtimes
client_max_body_size 100m;
client_body_timeout 120s;
sendfile off;
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;
ssl_session_cache shared:SSL:10m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
ssl_prefer_server_ciphers on;
# See https://hstspreload.org/ before uncommenting the line below.
# add_header Strict-Transport-Security "max-age=15768000; preload;";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
add_header Content-Security-Policy "frame-ancestors 'self'";
add_header X-Frame-Options DENY;
add_header Referrer-Policy same-origin;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "upload_max_filesize = 100M \n post_max_size=100M";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTP_PROXY "";
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
include /etc/nginx/fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}