贡献者: addis
sudo apt install nginx
sudo systemctl restart nginx
curl localhost
或者 curl http://localhost
(默认访问 80 接口)。如果打印出一个 html 文本,包含 Welcome to nginx!
,就是成功了(当然也可以用浏览器访问,只是有时候只有命令行)。
sudo vim /etc/nginx/sites-enabled/default
,然后在 listen 80 default_server;
的 80
改成 网卡ip:80
,然后重启 nginx
服务即可生效。
/etc/nginx
配置文件(为了确保可以先把这个文件夹删掉),用 apt purge nginx nginx-common nginx-full
,然后 apt install nginx
。
/etc/nginx/nginx.conf
。在 http
section 里面加入
server {
listen 80;
server_name 公网ip或域名;
location / {
root /静态网页根目录;
}
}
静态网页根目录
以及它的所有上层目录需要可以被 nginx 的用户 www-data
读取和执行,里面的文件也一样。如果权限不对访问网页会出现错误 403 forbidden
。
ps aux | grep nginx
,看第一列中除了 root
都有哪些用户。
curl 公网ip或域名
,如果 ip
不是公网 ip 就只能在局域网的机器上访问。如果机器上有 GUI 浏览器,也可以直接在网址栏输入 公网ip或域名
。
公网ip或域名
中可以使用任何域名不需要注册。
80
端口可以访问:http://域名
。
sudo snap install core; sudo snap refresh core
sudo apt remove certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot certonly --nginx
,这时会互动提示输入域名等信息。
sudo certbot renew --dry-run
/etc/letsencrypt/live/域名
中:cert.pem chain.pem fullchain.pem privkey.pem
http { ... }
的最后添加一个:
server {
listen 443 ssl;
server_name 域名;
ssl_certificate /etc/letsencrypt/live/域名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/域名/privkey.pem;
location / {
root /var/www/html;
index index.html;
}
}
systemctl restart nginx.service
https://域名
/etc/nginx/nginx.conf
中插入新的 section(包括下面的反向代理 section),同样先添加 80 端口,输入不同的域名和目录,重启 nginx 即可。
比如你在国内访问 github 很慢,但在美国有一个私人服务器,那么你可以用 Nginx 作为反向代理,这样你就可以访问你代理服务器的域名了。
同样只需要在设置中添加一个 server section 即可
如果只需要 http 代理,用
server {
listen 80;
server_name 公网ip或域名;
location / {
proxy_pass https://github.com/;
proxy_set_header Host github.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
如果需要 https 代理,用
server {
listen 443 ssl;
server_name 域名;
ssl_certificate /etc/letsencrypt/live/域名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/域名/privkey.pem;
location / {
proxy_pass https://github.com/;
proxy_set_header Host github.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
尝试了对 wikipedia 也进行反向代理,但根据 wikipedia 的设置浏览器 ulr 总是会跳转回 wikipedia.org,GPT-4 建议在 location 中添加以下设置,但仍然无效。这可能超出了 nginx 的能力范围。
proxy_set_header Referer "";
proxy_redirect https://wikipedia.org/ $scheme://$host/;
# Enable URL rewriting
sub_filter_once off;
sub_filter_types *;
sub_filter 'https://wikipedia.org' '$scheme://$host';
sub_filter 'https://www.wikipedia.org' '$scheme://$host';
 
 
 
 
 
 
 
 
 
 
 
友情链接: 超理论坛 | ©小时科技 保留一切权利