尽量使用普通用户搭建nginx服务

创建用户

# 可以使用adduser或useradd,但useradd需要手动配置
adduser mywind

安装nginx

安装相关依赖及nginx包

# 安装gcc, make,这里必须使用root权限
apt install gcc
apt install make
# 下载相关依赖并解压
# 下载及解压pcre
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar zxvf pcre-8.43.tar.gz
# 下载及解压zlib
wget http://zlib.net/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz
# 下载及解压openssl
wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
tar zxvf openssl-1.0.2s.tar.gz
# 下载及解压nginx
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar zxvf nginx-1.16.1.tar.gz

开始安装

# 切换到目录
cd nginx-1.16.1
# 配置
./configure \
    --prefix=../nginx \
    --with-pcre=../pcre-8.43 \
    --with-zlib=../zlib-1.2.11 \
    --with-openssl=../openssl-1.0.2s \
    --user=root \
    --group=root \
    --with-file-aio \
    --with-http_v2_module \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_sub_module \
    --with-http_gzip_static_module \
    --with-http_stub_status_module

# 编译 && 安装
make 
make install

启动及配置

安装完后会生成nginx文件夹

# 先切换到sbin 目录
cd /home/mywind/nginx/sbin
# 设置权限(需要使用root权限),若不绑定80端口,可以不进行操作
# 主要是监听1024以下的端口需要root权限
chown root:root nginx
chmod 755 nginx
# 启动
cd /home/mywind/nginx
./sbin/nginx
# 检查配置文件
./sbin/nginx -t
# 重启nginx
./sbin/nginx -s reload
# 终止nginx服务
./sbin/nginx -s stop

简单的nginx配置-配置两个服务器

配置文件

如果要绑定80端口,启动nginx时仍需要root权限

 server {
        listen       8080;
        server_name  www.a.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  indexA.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    server {
        listen       8000;
    #   listen       somename:8080;
    #   server_name  somename  alias  another.alias;
        server_name  www.b.com;

        location / {
            root   html;
            index  indexB.html index.htm;
        }
    }

对应的/html下有indexA.html,indexB.html

再改一下windows系统中的hosts文件,达到本地域名解析的效果

再启动nginx,便可以达到想要的效果

其实不用端口号区别也可以

对应的配置文件

由于绑定的是80端口,所以需要root权限

 server {
        listen       80;
        server_name  www.a.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  indexA.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    server {
        listen       80;
    #   listen       somename:8080;
    #   server_name  somename  alias  another.alias;
        server_name  www.b.com;

        location / {
            root   html;
            index  indexB.html index.htm;
        }
    }

效果