nginx 安装及配置

74 阅读1分钟

nginx 安装

查询是否安装过 nginx

rpm -qa | grep nginx

配置 nginx 安装包源

sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装 nginx

yum install -y nginx

启动 nginx

systemctl start nginx

启动后,在浏览器中输入服务器地址,如果可以出现欢迎页面,即表示安装成功。 image.png

设置开机自启动

sudo systemctl enable nginx

执行上面的命令后,如果想要查看设置开机启动是否成功,可以用以下命令进行查询。

systemctl list-unit-files | grep nginx

nginx 反向代理配置

开启防火墙端口

查询开启的端口号

firewall-cmd --zone=public --list-ports

开启80端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

配置反向代理

vim /etc/nginx/conf.d/default.conf

把想要配置的端口加入到nginx服务

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    # 8081 管理后台
    location /musicserver/ {
        #alias /root;
	proxy_pass http://localhost:8081/;#代理的实际端口,在本地8001
	proxy_set_header Host $host;# 代理请求信息,转换为实际域名地址
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_connect_timeout 15s;
    }
	# 8082管理后台 -- 处理 websocket连接
    location /gomoku/ {
        proxy_pass http://localhost:8082/;
	proxy_set_header Host $host;# 代理请求信息,转换为实际域名地址	
	proxy_http_version 1.1;
    	proxy_set_header Upgrade $http_upgrade;
    	proxy_set_header Connection "Upgrade";
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    	proxy_set_header X-Real-IP $remote_addr;    
}

    #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   /usr/share/nginx/html;
    }

    #location ~ .*\.(js|css|jpg|jpeg|gif|png|ico|pdf|txt)$ {
	#	proxy_pass http://127.0.0.1:8081;
    #}

    # 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;
    #}
}


这里配置后虽然能访问静态网页了,但是ajax请求还是缺少 /a/ 路径, 需要在前端代码中的请求路径中添加/a/。