Nginx反向代理实现域名不带端口访问网站
配置环境:Ubuntu 20.04 server
基本操作
安装nginx
apt update
apt install nginx
启动nginx
nginx
访问主域名,若看到如下图片则启动成功
关闭nginx
nginx -s stop
重启nginx
nginx -s reload
配置反向代理
进入安装nginx的文件夹,以/etc/nginx/为例
cd /etc/nginx/
打开配置文件
vim nginx.conf
修改配置文件如下
注意:
请修改配置文件中的域名及代理的ip地址及端口。
以下文件中分别对两个不同的端口进行了反向代理,如过只需代理一个端口,请注释掉Virtual Host Configs中另一个端口的代理配置。
同理,若想代理多个不同的端口到不同的二级域名下,可以新增server配置。
其中log功能以及其他功能可以按需开启。
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
server {
listen 80;
server_name cloud.xxx.cn; #修改为域名
# access_log logs/cloud_logs/access.log;
# 1error_log logs/cloud_logs/error.log;
location / {
proxy_pass http://43.xxx.xxx.xxx:xxxx; #修改为ip + 端口
}
}
server {
listen 80;
server_name mc.xxx.cn; #修改为域名
# access_log logs/mc_logs/access.log;
# error_log logs/mc_logs/error.log;
location / {
proxy_pass http://43.xxx.xxx.xxx:xxxx; #修改为ip + 端口
}
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
保存配置文件,然后重启ngnix
测试
访问对应的域名网站,若看见成功打开网页则完成。
可以选择备份nginx文件以备使用
cp nginx.conf nginx.conf.bak